quick sort(重复数版)

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

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 }
原文地址:https://www.cnblogs.com/endenvor/p/9655350.html