码迷,mamicode.com
首页 > 其他好文 > 详细

quick sort(重复数版)

时间:2018-09-16 15:48:11      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:ref   ret   bsp   oid   int   cat   for   col   amp   

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

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(重复数版)

标签:ref   ret   bsp   oid   int   cat   for   col   amp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!