快速排序的标准代码

 1 template <class T, class LessThan>
 2 void sort(T* array, int size, LessThan lt)
 3 {
 4     if (size <= 15)
 5         selectionSort(array, size, lt);
 6 
 7     else{
 8         T           pivot = array[size / 2];
 9         T           tmp;
10         int         i = -1;
11         int         j = size;
12 
13         for(;;){
14             do i++; while(lt(array[i], pivot));
15             do j--; while(lt(pivot, array[j]));
16 
17             if (i >= j) break;
18 
19             tmp = array[i]; array[i] = array[j]; array[j] = tmp;
20         }
21 
22         sort(array    , i     , lt);
23         sort(&array[i], size-i, lt);
24     }
25 }
26 template <class T> static inline void sort(T* array, int size) { 27 sort(array, size, LessThan_default<T>()); }

 

   
 
1 struct VarSlsInfo { int index; int flipN; };
2 static inline VarSlsInfo mkVarSlsInfo(int _index, int _flipN)
{ VarSlsInfo d = {_index, _flipN}; return d; }
3 4 struct VarSlsInfoLessThan { 5 const vec<VarSlsInfo>& _allVarsInfoSLS; 6 bool operator () (VarSlsInfo x, VarSlsInfo y) const { return x.flipN > x.flipN; } 7 VarSlsInfoLessThan(const vec<VarSlsInfo>& _act) : _allVarsInfoSLS(_act) { } 8 };

假定已知排序序列为vec<VarSlsInfo> allVarsInfoSLS;,其元素类型为VarSlsInfo

使用排序的语句如下:

   
posted on 2022-09-13 14:03  海阔凭鱼跃越  阅读(27)  评论(0编辑  收藏  举报