采用三数取中,并用0下标元素存储枢轴。
- //CodeBy Pnig0s1992
- #include <iostream>
- using namespace std;
- int Partition(int * L,int low,int high);
- void QuickSort(int * L,int low,int high);
- int main(int argc,char * argv[])
- {
- int L[10]={0,50,10,90,30,70,40,80,60,20};
- QuickSort(L,1,9);
- for (int index=1;index<=9;index++)
- {
- cout<<L[index]<<endl;
- }
- system("pause");
- return 0;
- }
- void QuickSort(int * L,int low,int high)
- {
- int privot;
- if(low<high)
- {
- privot = Partition(L,low,high);
- QuickSort(L,1,privot-1);
- QuickSort(L,privot+1,high);
- }
- }
- int Partition(int * L,int low,int high)
- {
- int privo;
- int m = low+(high-low)/2;
- if(L[high]<L[m])
- swap(L[m],L[high]);
- if(L[high]<L[low])
- swap(L[low],L[high]);
- if(L[m]>L[low])
- swap(L[m],L[low]);
- privo = L[low];
- L[0] = privo;
- while(low<high)
- {
- while (low<high&&L[high]>=privo)
- {
- high--;
- }
- L[low] = L[high];
- //swap(L[low],L[high]);
- while(low<high&&L[low]<=privo)
- {
- low++;
- }
- L[high] = L[low];
- //swap(L[low],L[high]);
- }
- L[low] = L[0];
- return low;
- }