006.交换排序—快速排序(Quick Sort)

基本思想:

1)选择一个基准元素,通常选择第一个元素或者最后一个元素,

2)通过一趟排序讲待排序的记录分割成独立的两部分,其中一部分记录的元素值均比基准元素值小。另一部分记录的 元素值比基准值大。

3)此时基准元素在其排好序后的正确位置

4)然后分别对这两部分记录用同样的方法继续进行排序,直到整个序列有序。

快速排序的示例:

(a)一趟排序的过程:

(b)排序的全过程


算法的实现:

扫描二维码关注公众号,回复: 1904442 查看本文章

 递归实现:

  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. }  

猜你喜欢

转载自blog.csdn.net/qq_20398345/article/details/80917502