随机快速排序

来源:互联网 发布:windows phone登录 编辑:程序博客网 时间:2024/06/08 01:12

随机快速排序算法是对快速算法的一种优化,本质没什么区别,随机快速排序的最坏情况就是和快速排序一样。

上代码:

 

[cpp] view plaincopy
  1. // Randomizde_QuickSort.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include<iostream>  
  6. #include<ctime>  
  7. using namespace std;  
  8.   
  9. /*函数声明*/  
  10. void Randomize_QuickSort(int *A,int p,int r);   //随机快速排序  
  11. int Randomize_Partition(int *A,int p,int r);        //随机分治法  
  12. int Partition(int *A,int p,int r);   //分治法  
  13. void Display(int *a,int size);          //打印函数  
  14.   
  15. /*主函数*/  
  16. int _tmain(int argc, _TCHAR* argv[])  
  17. {  
  18.     int size,*a;  
  19.     while(1)  
  20.     {  
  21.         cout<<"输入字符串长度:"<<endl;  
  22.         cin>>size;  
  23.         if(size > 0) {  
  24.             cout<<"请输入"<<size<<"个待排序数字:"<<endl;  
  25.             a = (int*)malloc(size*sizeof(int)); //a = new int [size];  
  26.             for(int i=0; i<size; i++)  //输入数组  
  27.             {  
  28.                 cin>>a[i];  
  29.             }  
  30.             Randomize_QuickSort(a,0,size-1); //调用快速排序函数  
  31.         }  
  32.         else  
  33.             cout<<"输入长度错误!"<<endl;  
  34.   
  35.         Display(a,size);   //打印数组  
  36.     }  
  37.     return 0;  
  38. }  
  39.   
  40. /*函数定义*/  
  41. void Randomize_QuickSort(int *A,int p,int r)    //随机快速排序  
  42. {  
  43.     int q;  
  44.     if(p<r)               //如果p 小于等于 r 那么就程序不执行  
  45.     {  
  46.         q = Randomize_Partition(A,p,r);  //调用随机分治法 找到q的值  
  47.         Randomize_QuickSort(A,p,q-1);  
  48.         Randomize_QuickSort(A,q+1,r);  
  49.     }  
  50. }  
  51.   
  52. int Randomize_Partition(int *A,int p,int r) //随机分治法  
  53. {                                                 
  54.     int i,temp;  
  55.     srand(time(0)); /*设置种子,并生成伪随机序列*/  
  56.     i = p+(int)((r-p+1)*rand()/(RAND_MAX+1.0));  
  57.       
  58.     temp = A[r]; //exchange  
  59.     A[r] = A[i];  
  60.     A[i] = temp;  
  61.   
  62.     return Partition(A,p,r);  //返回q值  
  63. }  
  64.   
  65. int Partition(int *A,int p,int r) //分治法  
  66. {                                                     
  67.     int x,i,j,temp;  
  68.   
  69.     x = A[r];  //将最后一个值保存在x中  
  70.     i = p-1;   //开始的时候将i 移动到数组的外面  
  71.     for( j=p; j<=r-1; j++)  
  72.     {  
  73.         if(A[j]<=x)  
  74.         {  
  75.             i +=1;  
  76.             temp = A[i]; //exchange  
  77.             A[i] = A[j];  
  78.             A[j] = temp;  
  79.         }  
  80.     }  
  81.   
  82.     temp = A[i+1];  //exchange  
  83.     A[i+1] = A[r];  
  84.     A[r] = temp;  
  85.   
  86.     return i+1;  //返回q值  
  87. }  
  88.   
  89. void Display(int *a,int size)  //打印函数  
  90. {  
  91.         cout<<"排序结果为:"<<endl;  
  92.         for(int i=0; i<size; i++)    //打印数组  
  93.         {  
  94.             cout<<a[i]<<" ";  
  95.         }  
  96.         cout<<endl<<endl;  
  97. }  



    下面我们测试一组数组: 2 8 7 1 3 5 6 4