首页 > 其他 > 详细

quick sort(重复数版)

时间:2018-09-16 13:41:49      阅读:180      评论:0      收藏:0      [点我收藏+]

针对数组中有大量重复数优化

example

// 1,3,4,7,7,7,17,11,1,7

 1 void QuickSort(int* arr, int from, int to);
 2 void QSort(int* arr, int size);
 3 
 4 void QuickSort(int* arr, int from, int to)
 5 {
 6     if(from >= to)    return;
 7     int pivot = arr[to];
 8     // save the numbers of who‘s value equals pivot
 9     int numsOfPivot = 0;
10     int border = from;
11 //                b     i
12 //    1,3,4,7,7,7,17,11,1,7
13     for(int i = from; i <= to; i++)
14     {
15         if(arr[i] < pivot)
16         {
17             int temp = arr[i];
18             arr[i] = arr[border];
19             arr[border - numsOfPivot] = temp;
20             ++border;
21         }
22         else if(arr[i] == pivot)
23         {
24             ++numsOfPivot;
25             arr[i] = arr[border];
26             ++border;
27         }
28     }
29     // refill the duplicate of pivot at the behind of border
30     while(numsOfPivot != 0)
31     {
32         arr[border-numsOfPivot] = pivot;
33         --numsOfPivot;
34     }
35     QuickSort(arr,from,border-2-numsOfPivot);
36     QuickSort(arr,border,to);
37 }
38 
39 void QSort(int* arr, int size)
40 {
41     QuickSort(arr, 0, size-1);
42 }

 

quick sort(重复数版)

原文:https://www.cnblogs.com/endenvor/p/9655350.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!