排序算法之快速排序

快速排序(Quick Sort)

基本思想:

1)選擇一個基準元素,通常選擇第一個元素或者最後一個元素,

2)通過一趟排序講待排序的記錄分割成獨立的兩部分,其中一部分記錄的元素值均比基準元素值小。另一部分記錄的 元素值比基準值大。

3)此時基準元素在其排好序後的正確位置

4)然後分別對這兩部分記錄用同樣的方法繼續進行排序,直到整個序列有序。

快速排序的示例:

(a)一趟排序的過程:

(b)排序的全過程


算法的實現:

 遞歸實現:

  1. void print(int a[], int n){  
  2.     for(int j= 0; j<n; j++){  
  3.         cout<<a[j] <<"  ";  
  4.     }  
  5.     cout<<endl;  
  6. }  
  7.   
  8. void swap(int *a, int *b)  
  9. {  
  10.     int tmp = *a;  
  11.     *a = *b;  
  12.     *b = tmp;  
  13. }  
  14.   
  15. int partition(int a[], int low, int high)  
  16. {  
  17.     int privotKey = a[low];                             //基準元素  
  18.     while(low < high){                                   //從表的兩端交替地向中間掃描  
  19.         while(low < high  && a[high] >= privotKey) --high;  //從high 所指位置向前搜索,至多到low+1 位置。將比基準元素小的交換到低端  
  20.         swap(&a[low], &a[high]);  
  21.         while(low < high  && a[low] <= privotKey ) ++low;  
  22.         swap(&a[low], &a[high]);  
  23.     }  
  24.     print(a,10);  
  25.     return low;  
  26. }  
  27.   
  28.   
  29. void quickSort(int a[], int low, int high){  
  30.     if(low < high){  
  31.         int privotLoc = partition(a,  low,  high);  //將表一分爲二  
  32.         quickSort(a,  low,  privotLoc -1);          //遞歸對低子表遞歸排序  
  33.         quickSort(a,   privotLoc + 1, high);        //遞歸對高子表遞歸排序  
  34.     }  
  35. }  
  36.   
  37. int main(){  
  38.     int a[10] = {3,1,5,7,2,4,9,6,10,8};  
  39.     cout<<"初始值:";  
  40.     print(a,10);  
  41.     quickSort(a,0,9);  
  42.     cout<<"結果:";  
  43.     print(a,10);  
  44.   
  45. }  


分析:

快速排序是通常被認爲在同數量級(O(nlog2n))的排序方法中平均性能最好的。但若初始序列按關鍵碼有序或基本有序時,快排序反而蛻化爲冒泡排序。爲改進之,通常以“三者取中法”來選取基準記錄,即將排序區間的兩個端點與中點三個記錄關鍵碼居中的調整爲支點記錄。快速排序是一個不穩定的排序方法。

 
快速排序的改進

在本改進算法中,只對長度大於k的子序列遞歸調用快速排序,讓原序列基本有序,然後再對整個基本有序序列用插入排序算法排序。實踐證明,改進後的算法時間複雜度有所降低,且當k取值爲 8 左右時,改進算法的性能最佳。算法思想如下:

  1. void print(int a[], int n){  
  2.     for(int j= 0; j<n; j++){  
  3.         cout<<a[j] <<"  ";  
  4.     }  
  5.     cout<<endl;  
  6. }  
  7.   
  8. void swap(int *a, int *b)  
  9. {  
  10.     int tmp = *a;  
  11.     *a = *b;  
  12.     *b = tmp;  
  13. }  
  14.   
  15. int partition(int a[], int low, int high)  
  16. {  
  17.     int privotKey = a[low];                 //基準元素  
  18.     while(low < high){                   //從表的兩端交替地向中間掃描  
  19.         while(low < high  && a[high] >= privotKey) --high; //從high 所指位置向前搜索,至多到low+1 位置。將比基準元素小的交換到低端  
  20.         swap(&a[low], &a[high]);  
  21.         while(low < high  && a[low] <= privotKey ) ++low;  
  22.         swap(&a[low], &a[high]);  
  23.     }  
  24.     print(a,10);  
  25.     return low;  
  26. }  
  27.   
  28.   
  29. void qsort_improve(int r[ ],int low,int high, int k){  
  30.     if( high -low > k ) { //長度大於k時遞歸, k爲指定的數  
  31.         int pivot = partition(r, low, high); // 調用的Partition算法保持不變  
  32.         qsort_improve(r, low, pivot - 1,k);  
  33.         qsort_improve(r, pivot + 1, high,k);  
  34.     }   
  35. }   
  36. void quickSort(int r[], int n, int k){  
  37.     qsort_improve(r,0,n,k);//先調用改進算法Qsort使之基本有序  
  38.   
  39.     //再用插入排序對基本有序序列排序  
  40.     for(int i=1; i<=n;i ++){  
  41.         int tmp = r[i];   
  42.         int j=i-1;  
  43.         while(tmp < r[j]){  
  44.             r[j+1]=r[j]; j=j-1;   
  45.         }  
  46.         r[j+1] = tmp;  
  47.     }   
  48.   
  49. }   
  50.   
  51.   
  52.   
  53. int main(){  
  54.     int a[10] = {3,1,5,7,2,4,9,6,10,8};  
  55.     cout<<"初始值:";  
  56.     print(a,10);  
  57.     quickSort(a,9,4);  
  58.     cout<<"結果:";  
  59.     print(a,10);  
  60.   
  61. }  
  62. 轉:http://blog.csdn.net/cangchen/article/details/44816905
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章