分治法 --快速排序

来源:互联网 发布:ubuntu变为中文 编辑:程序博客网 时间:2024/06/05 09:23

【分治法】快速排序

      1、未优化的快速排序 

      快速排序的基本思想是通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列

   程序清单如下:

[cpp] view plaincopy
  1. //2d8-1 未优化的快速排序  
  2. #include "stdafx.h"  
  3. #include <iostream>       
  4. using namespace std;   
  5.   
  6. int a[] = {5,7,3,4,8,6,9,1,2};  
  7.   
  8. template <class Type>  
  9. void Swap(Type &x,Type &y);  
  10.   
  11. template <class Type>  
  12. int Partition(Type a[],int p,int r);  
  13.   
  14. template <class Type>  
  15. void QuickSort(Type a[],int p,int r);  
  16.   
  17. int main()  
  18. {  
  19.     for(int i=0; i<9; i++)  
  20.     {  
  21.         cout<<a[i]<<" ";  
  22.     }  
  23.     cout<<endl;  
  24.     QuickSort(a,0,8);  
  25.     for(int i=0; i<9; i++)  
  26.     {  
  27.         cout<<a[i]<<" ";  
  28.     }  
  29.     cout<<endl;  
  30. }  
  31.   
  32. template <class Type>  
  33. void Swap(Type &x,Type &y)  
  34. {  
  35.     Type temp = x;  
  36.     x = y;  
  37.     y = temp;  
  38. }  
  39.   
  40. template <class Type>  
  41. int Partition(Type a[],int p,int r)  
  42. {  
  43.     int i = p,j = r + 1;  
  44.     Type x = a[p];  
  45.   
  46.     while(true)  
  47.     {  
  48.         while(a[++i]<x && i<r);  
  49.         while(a[--j]>x);  
  50.         if(i>=j)  
  51.         {  
  52.             break;  
  53.         }  
  54.         Swap(a[i],a[j]);  
  55.     }  
  56.     a[p] = a[j];  
  57.     a[j] = x;  
  58.     return j;  
  59. }  
  60.   
  61. template <class Type>  
  62. void QuickSort(Type a[],int p,int r)  
  63. {  
  64.     if(p<r)  
  65.     {  
  66.         int q = Partition(a,p,r);  
  67.         QuickSort(a,p,q-1);  
  68.         QuickSort(a,q+1,r);  
  69.     }  
  70. }  

     程序解释:第48、49行下标j逐渐减小,i逐渐增大,直到a[i]>=x>=a[j]。第50行若i<j,交换a[i]与a[j]的位置,扩展左右两个区域。需要注意的是第56行与第57行:由于while(a[--j]>x);执行后,j会多减一下,最后指向a[j]会小于x,这时候将a[p]的值与a[j]交换刚好满足左侧都比x小,右侧都比x大

   2、随机后标记元素后的快速排序

   未随机化处理的快速排序最坏情况发生在划分过程产生的两个区域分别包含n-1个元素和1个元素的时候。由于函数Partition的计算时间为O(n),所以如果算法Partition的每一步都出现这种不对称划分,则T(n)=O(n^2)。

   在最好情况下,每次划分所取的基准都恰好为中值,即每次划分都产生两个大小为n/2的区域。此时T(n)=O(nlogn)。

   因此,快速排序算法的性能取决于划分的对称性修改Partition,可以设计出采用随机选择策略的快速排序算法。在快速排序算法的每一步中,当数组还没有被划分时,可以在a[p:r]中随机选择一个元素作为划分基准。

   程序如下:

[cpp] view plaincopy
  1. //2d8-2 随机后标记元素后的快速排序  
  2. #include "stdafx.h"  
  3. #include <iostream>   
  4. #include <ctime>  
  5. using namespace std;   
  6.   
  7. int a[] = {5,7,3,4,8,6,9,1,2};  
  8.   
  9. template <class Type>  
  10. void Swap(Type &x,Type &y);  
  11.   
  12. inline int Random(int x, int y);  
  13.   
  14. template <class Type>  
  15. int Partition(Type a[],int p,int r);  
  16.   
  17. template<class Type>  
  18. int RandomizedPartition(Type a[],int p,int r);  
  19.   
  20. template <class Type>  
  21. void RandomizedQuickSort(Type a[],int p,int r);  
  22.   
  23. int main()  
  24. {  
  25.     for(int i=0; i<9; i++)  
  26.     {  
  27.         cout<<a[i]<<" ";  
  28.     }  
  29.     cout<<endl;  
  30.     RandomizedQuickSort(a,0,8);  
  31.     for(int i=0; i<9; i++)  
  32.     {  
  33.         cout<<a[i]<<" ";  
  34.     }  
  35.     cout<<endl;  
  36. }  
  37.   
  38. template <class Type>  
  39. void Swap(Type &x,Type &y)  
  40. {  
  41.     Type temp = x;  
  42.     x = y;  
  43.     y = temp;  
  44. }  
  45.   
  46. inline int Random(int x, int y)  
  47. {  
  48.      srand((unsigned)time(0));  
  49.      int ran_num = rand() % (y - x) + x;  
  50.      return ran_num;  
  51. }  
  52.   
  53. template <class Type>  
  54. int Partition(Type a[],int p,int r)  
  55. {  
  56.     int i = p,j = r + 1;  
  57.     Type x = a[p];  
  58.   
  59.     while(true)  
  60.     {  
  61.         while(a[++i]<x && i<r);  
  62.         while(a[--j]>x);  
  63.         if(i>=j)  
  64.         {  
  65.             break;  
  66.         }  
  67.         Swap(a[i],a[j]);  
  68.     }  
  69.     a[p] = a[j];  
  70.     a[j] = x;  
  71.     return j;  
  72. }  
  73.   
  74. template<class Type>  
  75. int RandomizedPartition(Type a[],int p,int r)  
  76. {  
  77.     int i = Random(p,r);  
  78.     Swap(a[i],a[p]);  
  79.     return Partition(a,p,r);  
  80. }  
  81.   
  82. template <class Type>  
  83. void RandomizedQuickSort(Type a[],int p,int r)  
  84. {  
  85.     if(p<r)  
  86.     {  
  87.         int q = RandomizedPartition(a,p,r);  
  88.         RandomizedQuickSort(a,p,q-1);  
  89.         RandomizedQuickSort(a,q+1,r);  
  90.     }  
  91. }  

0 0
原创粉丝点击