快速排序/随机快速排序

Posted Y_SL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速排序/随机快速排序相关的知识,希望对你有一定的参考价值。

快速排序是一个递归算法,重点是Partition()函数的实现过程。随机快速排序中调用Randomized_Partition(),此函数的实现只需要在Partition()的基础上前面多一个随机化和交换的过程。对于无序数据,因为随机快排需要执行随机化的过程,故性能低于普通快排。对于有序数据,普通快排是最坏情况,性能最低,尽管随机快排仍要执行随机化过程,但因避免了最坏的情况,性能比普通快排好。整体来看,两种快排在处理有序数据时性能均低于处理无序数据

 

int Partition(int*A,int p,int r);

int Randomized_Partition(int*A,int p,int r); 

void Quicksort(int *A,int p,int r);

void Randomized_Quicksort(int*A,int p,int r);

void Swap(int &a,int &b);
Quicksort.h
  1 #include<iostream>
  2 #include<time.h> 
  3 #include<stdio.h>
  4 #include <stdlib.h>
  5 #include<fstream>
  6 using namespace std;
  7 
  8 #include"Quicksort.h"
  9 
 10 #define N 10000
 11 
 12 int Num[N];
 13 int choice;
 14 double start,finish;
 15 
 16 int main(void)
 17 {
 18     
 19     // Write N random numbers to data.txt
 20     ofstream in;
 21     in.open("data.txt",ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建
 22     int x;
 23     srand((unsigned) time(NULL));
 24     cout<<"Numbers needed to be sort are:"<<endl;
 25     for(int n=0;n<N;n++)
 26     {        
 27         x = rand() %100;
 28         //cout<<x<<" ";
 29         in<<x<<" ";
 30         Num[n] = x;
 31         
 32     }
 33     cout<<endl;
 34     in.close(); //勿忘关闭文件
 35                 /*
 36                 // Read data from data.txt
 37                 ifstream fin("data.txt");
 38                 vector<int> vec;
 39                 
 40                   //fin Input value into idata,like cin>>a , cin get value from keyboard,fin get value from file
 41                   //This reading method won\'t get twice of the last data
 42                   int idata;
 43                   fin >> idata;
 44                   while (fin)  
 45                   {
 46                   vec.push_back(idata);
 47                   fin >> idata;
 48                   }
 49                   
 50                     //N = vec.size();
 51                     for(int i=0;i<N;i++)
 52                     {
 53                     Num[i]=vec[i];
 54                     }
 55     */    
 56     cout<<"1: Quicksort  Other: Randomized_Quick"<<endl<<"Your Choice: ";
 57     cin>>choice;
 58     
 59     //General Quicksort 
 60     start = clock();//
 61     if(choice == 1)
 62     {
 63         Quicksort(Num,0,N-1);
 64         finish = clock();//
 65         
 66         //Output to screen & ResultData.txt
 67         cout<<"Your Quciksort Result is:"<<endl;
 68         in.open("ResultData.txt",ios::trunc);
 69         for(int i=0;i<N;i++)
 70         {
 71             //cout<<Num[i]<<" ";
 72             in<<Num[i]<<" ";
 73         }
 74         cout<<endl;
 75         in.close();
 76         cout<<"number of ticks in Quicksort is: "<<(finish-start)/CLOCKS_PER_SEC<<endl;//
 77         return 0;
 78     }
 79     
 80     //Randomized_Quicksort
 81     Randomized_Quicksort(Num,0,N-1);
 82     
 83     finish = clock();//
 84     //clock_t finish = clock();//
 85     
 86     //Output to screen
 87     cout<<"Your Randomized_Quciksort Result is:"<<endl;
 88     in.open("ResultData.txt",ios::trunc);
 89     for(int i=0;i<N;i++)
 90     {
 91         //cout<<Num[i]<<" ";
 92         in<<Num[i]<<" ";
 93     }
 94     cout<<endl;
 95     in.close();
 96     /*
 97     for(i=0;i<N;i++)
 98     {
 99     cout<<Num[i]<<" ";
100     }
101     cout<<endl;
102     */    
103     cout<<"number of ticks in Randomized_Quicksort is: "<<(finish-start)/CLOCKS_PER_SEC<<endl;//
104     return 0;
105 }
main.cpp
 1 #include"Quicksort.h"
 2 int Partition(int*A,int p,int r)
 3 {    
 4     int x = A[r];
 5     int i = p-1;
 6     for(int j=p;j<r;j++)
 7     {
 8         if(A[j]<=x)
 9         {
10             i+=1;
11             if(i!=j)
12             {
13                 Swap(A[i],A[j]);
14             }
15         }
16     }
17     Swap(A[i+1],A[r]);
18     return i+1;
19 
20 }
Partition.cpp
 1 #include"Quicksort.h"
 2 void Quicksort(int *A,int p,int r)
 3 {
 4     if(p<r)
 5     {
 6         int q = Partition(A,p,r);
 7         Quicksort(A,p,q-1);
 8         Quicksort(A,q+1,r);
 9     }        
10 }
Quicksort.cpp
 1 #include<time.h>
 2 #include<stdlib.h>
 3 #include"Quicksort.h"
 4 
 5 int Randomized_Partition(int*A,int p,int r)
 6 {
 7     srand((unsigned) time(NULL));
 8     int k = -1;
 9     while(k<p)
10     {
11         k = rand() % (r+1);
12     }
13     Swap(A[k],A[r]);
14     //Partition(A,p,r);
15     int x = A[r];
16     int i = p-1;
17     for(int j=p;j<r;j++)
18     {
19         if(A[j]<=x)
20         {
21             i+=1;
22             if(i!=j)
23             {
24                 Swap(A[i],A[j]);
25             }
26         }
27     }
28     Swap(A[i+1],A[r]);
29     return i+1;
30 }
Randomized_Partition.cpp
 1 #include<iostream.h>
 2 #include"Quicksort.h"
 3 void Randomized_Quicksort(int *A,int p,int r)
 4 {
 5     if(p<r)
 6     {
 7         int q = Randomized_Partition(A,p,r);
 8         Randomized_Quicksort(A,p,q-1);
 9         Randomized_Quicksort(A,q+1,r);
10     }
11     
12 }
Randomized_Quicksort.cpp

 

以上是关于快速排序/随机快速排序的主要内容,如果未能解决你的问题,请参考以下文章

随机化快速排序法

快速排序算法详解

快速排序的优化随机化快速排序

快速排序(经典快排以及随机快排)

Python 实现快速排序和随机快速排序

混合快速/合并排序对随机数据的性能