快速排序详见大话数据结构p418
1 //快速排序 -->递归策略 2 3 void QuickSort(sqlist* L) 4 { 5 QSort(L,1,L->length); 6 7 } 8 9 void QSort(sqlist* L,int low,int high) 10 { 11 int pivot; 12 if(low<high) 13 { 14 pivot=Partition(L,low,high); 15 QSort(L,low,pivot); 16 QSort(L,pivot+1,high); 17 } 18 19 } 20 21 22 void Partition(sqlist* L,int low,int high) 23 { 24 int pivotkey; 25 pivotkey = L->r[low]; //将第一个值视为枢轴值,后面的数和枢轴值进行比较 26 whlie(low<high) 27 { 28 while(low<high && L->r[high]>= pivotkey) 29 --high; 30 swap(L,low,high); 31 while(low<high && L->r[low]<= pivotkey) 32 ++low; 33 swap(L,low,high); 34 35 36 } 37 return low; //low即为枢轴 38 }
浙公网安备 33010602011771号