快速排序及其优化

#include <iostream>
#include <algorithm>  //调用C++自带Sort
#include <crtdbg.h>
#include <cstdlib>
#include <windows.h> 
using namespace std;
#define MAX 1000000
#define K  12
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif // _DEBUG
// memory leak check
inline void EnableMemCheck()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
}
//插入排序声明
void InsertSort(int s[],int n);
int SelectMid(int a[]);

//划分函数
int Partion(int a[],int p,int r){
 int x=a[r],temp;
 int i=p-1,j=p;
 for(;j<r;j++){
	if(a[j]<x){
	i++;
	temp=a[j];
	a[j]=a[i];
	a[i]=temp;
	}
}
a[r]=a[i+1];
a[i+1]=x;

return i+1;

}

int BetterPartion(int a[],int p,int r){   //采用更优的划分
 int x=SelectMid(a),temp;
 int i=p-1,j=p;
 for(;j<r;j++){
	if(a[j]<x){
	i++;
	temp=a[j];
	a[j]=a[i];
	a[i]=temp;
	}
}
a[r]=a[i+1];
a[i+1]=x;

return i+1;

}


int SelectMid(int a[]){
	int b[5]={a[0],a[1],a[2],a[3],a[4]};
    InsertSort(b,5);
	return b[2];

}
//快速排序
void QuickSort(int a[],int p,int r){
	int q;
	if(p<r){
	q=Partion(a,p,r);
	QuickSort(a,p,q-1);
	QuickSort(a,q+1,r);
	}

}

//插入排序实现
void InsertSort(int s[],int n){
    int temp,i,j;  
    for (i=1;i<n;i++)  
    {  
        temp=s[i];  
        j=i;  
        while(temp<s[j-1]){  
          s[j]=s[j-1];  
          j--;  
        }  
        s[j]=temp;  
    } 
}



void BetterQuick(int a[],int p,int r){
	int q;
	if(p<r){
		if(r-p<K){         //当问题规模小于K时使插入排序
		InsertSort(a,r-p+1);  
		}
/*		else if(r-p>MAX/2){      //当问题规模较大时,采用更优的划分,效果不明显
	    q=BetterPartion(a,p,r);
		BetterQuick(a,p,q-1);
		BetterQuick(a,q+1,r);
		}**/
		else{
		q=Partion(a,p,r);
		BetterQuick(a,p,q-1);
		BetterQuick(a,q+1,r);
		}
     
	
	}


}

int main()
{
EnableMemCheck();
int *arr=new int[MAX];
int *arr1=new int[MAX];
int *arr2=new int[MAX];

for(int i=0; i<MAX; i++)
{
arr[i]=rand();
arr1[i]=arr[i];
arr2[i] = arr[i];
}

cout<<endl;
cout<<endl;
cout<<endl;
LARGE_INTEGER liFrequency;
LARGE_INTEGER liStart;
LARGE_INTEGER liEnd;
QueryPerformanceFrequency(&liFrequency); // get clock frequency

//调用优化过的QuickSort
QueryPerformanceCounter(&liStart); // time start
BetterQuick(arr, 0, MAX-1);
QueryPerformanceCounter(&liEnd); // time end
cout << "After BetterSort(" <<
(liEnd.QuadPart-liStart.QuadPart)*1000000/liFrequency.QuadPart
<< "μs): ";
//for(int i=0; i<MAX; i++) cout << arr[i] << " ";
cout << endl;
cout<<endl;
cout<<endl;



//调用QuickSort函数
QueryPerformanceCounter(&liStart); // time start
QuickSort(arr1,0,MAX-1);
QueryPerformanceCounter(&liEnd); // time end
cout << "the Quick Sort method takes :" <<
(liEnd.QuadPart-liStart.QuadPart)*1000000/liFrequency.QuadPart
<< "μs): ";
//for(int i=0; i<MAX; i++) cout << arr1[i] << " ";
cout << endl;


//调用C++自带的Sort函数
//QueryPerformanceCounter(&liStart); // time start
//std::sort(arr2,arr1+MAX);
//QueryPerformanceCounter(&liEnd); // time end
//cout << "the System Sort method takes :" <<
//(liEnd.QuadPart-liStart.QuadPart)*1000000/liFrequency.QuadPart
//<< "μs): ";
//for(int i=0; i<MAX; i++) cout << arr1[i] << " ";
cout << endl;

delete arr;
delete arr1;
delete arr2;
system("PAUSE");
return 0;
}

阅读更多
个人分类: 算法与趣味问题
想对作者说点什么? 我来说一句

java快速排序、随机优化快排

2016年05月12日 3KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭