数据结构——冒泡排序vs快速排序

冒泡排序和快速排序都是我们比较常用的排序方法(可能快速排序不是),这里进行了两种算法的效率比较,数量级为10,100,1K,10K,100K,1M。
代码如下:

#include 
#include 
#include //clock_t
#include //dword,large_integer
using namespace std;
int a[1000000];
fstream file;
void maopao(int a[1000000],int m){//冒泡排序 
	for(int i=0;i<m;i++){
		for(int j=i+1;j<m;j++){
			if(a[i]>a[j]){
				int b=a[i];
				a[i]=a[j];
				a[j]=b;
			}
		}
	}
}
void qsort(int a[], int low, int high){//快速排序 
    if(low >= high){
        return;
    }
    int first = low;
    int last = high;
    int key = a[first];/*用字表的第一个记录作为枢轴*/
    while(first < last){
        while(first < last && a[last] >= key){
            --last;
        } 
        a[first] = a[last];/*将比第一个小的移到低端*/ 
        while(first < last && a[first] <= key){
            ++first;
        }         
        a[last] = a[first];/*将比第一个大的移到高端*/
    }
    a[first] = key;/*枢轴记录到位*/
    qsort(a, low, first-1);
    qsort(a, first+1, high);
}
int main()
{	
	LARGE_INTEGER time,time0,time1;
	QueryPerformanceFrequency(&time);
	cout<<"冒泡排序为:"<<endl;
	file.open("10.txt",ios::in);
	int i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);	
	maopao(a,i);
	QueryPerformanceCounter(&time1);
	file.open("10_S.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为10的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("100.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);	
	maopao(a,i);
	QueryPerformanceCounter(&time1);
	file.open("100_S.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为100的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("1K.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);	
	maopao(a,i);
	QueryPerformanceCounter(&time1);
	file.open("1K_S.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为1K的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("10K.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);	
	maopao(a,i);
	QueryPerformanceCounter(&time1);
	file.open("10K_S.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为10K的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("100K.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);	
	maopao(a,i);
	QueryPerformanceCounter(&time1);
	file.open("100K_S.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为100K的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("1M.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);	
	maopao(a,i);
	QueryPerformanceCounter(&time1);
	file.open("1M_S.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为1M的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	cout<<"--------------------------------------------\n快速排序为:"<<endl;
	file.open("10.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);	
	qsort(a,0,i-1);
	QueryPerformanceCounter(&time1);
	file.open("10_M.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为10的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("100.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);	
	qsort(a,0,i-1);
	QueryPerformanceCounter(&time1);
	file.open("100_M.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为100的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("1K.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);		
	qsort(a,0,i-1);
	QueryPerformanceCounter(&time1);
	file.open("1K_M.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为1K的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("10K.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);		
	qsort(a,0,i-1);
	QueryPerformanceCounter(&time1);
	file.open("10K_M.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为10K的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("100K.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);		
	qsort(a,0,i-1);
	QueryPerformanceCounter(&time1);
	file.open("100K_M.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为100K的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	file.open("1M.txt",ios::in);
	i=0;
	while(!file.eof()){
		file>>a[i];
		i++;
	}
	file.close();
	QueryPerformanceCounter(&time0);		
	qsort(a,0,i-1);
	QueryPerformanceCounter(&time1);
	file.open("1M_M.txt",ios::out);
	for(int j=0;j<i;j++){
		file<<a[j]<<"\t";
	} 
	file.close();
	cout<<"数据规模为1M的时候时间为:"<<1000.0*(time1.QuadPart-time0.QuadPart)/time.QuadPart<<"ms"<<endl;
	return 0;
 } 
/*	clock_t time0,time1;
	DWORD time00,time11;
	LARGE_INTEGER time,time000,time111;
	time0=clock();
	time00=GetTickCount();
	QueryPerformanceFrequency(&time);
	QueryPerformanceCounter(&time000);
	for(int i=0;i<10000;i++){		
	}
	time1=clock();
	time11=GetTickCount();
	QueryPerformanceCounter(&time111);
	cout<<(double)(time1-time0)/CLOCKS_PER_SEC<<"s"<

结果如图:
数据结构——冒泡排序vs快速排序_第1张图片
比较分析:
【实验结果】
本次实验使用选择排序和快速排序进行测试,得到测试结果如下表所示: 单位(ms)
数据结构——冒泡排序vs快速排序_第2张图片
【结果分析】
实验环境是在Intel® Core™ i7-7700HQ处理器上,运行Windows10操作系统时,冒泡排序算法和快速排序算法的运行时间比较。
数据来源于Excel数学公式“=RANDBETWEEN()”,每次以1到样本值开始随机生成,比如样本数为10,“=RANDBETWEEN(1,10)”生成10个数据,将生成好的数据导入到对应的txt文本中。
通过结果可得,10到1M中6个样本值比较,冒泡排序运行时间每次以100倍的倍率进行突变,时间复杂度为O(n*n),在1M时,运行时间已经非常庞大,而升序排列和倒序排列(1K)时,感觉并没有什么区别,不管是升序排序还是倒序排序,运行时间相似。
而快速排序,每次以10倍的倍率进行突变,时间复杂度为O(nlogn),在UP和DOWN时由于轴值选择原因,不管是升序还是倒序序列,运行时间相似。
从两个算法比较来看,10以及100小样本数据中选择排序和快速排序差别不大,但是从1K往上,快速排序的优势就体现的即为明显,相差从一个数量级到4个数量级!可见快速排序在处理大容量数据时优势即为明显,而与数据的初始顺序无关,在小样本数据时,冒泡排序和快速排序运行时间上差别不大。

你可能感兴趣的:(数据结构)