快速排序和归并排序得比较,快排比归并慢很多?

HeartFang 2017-03-31 05:41:30
这是我的代码:
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

void merge(int A[],int low,int mid,int high);
void mergesort(int A[],int low,int high);
void split(int A[],int low,int high,int &w);
void quicksort(int A[],int low,int high);
void showsort(int A[],int low,int high);

int a[100001]; //进行排序的数组
int b[100001]; //辅助存储的数组
int B[100001]; //用于归并排序中辅助存储的数组
int count_merge = 0; //计算归并排序的比较次数,并初始化为0
int count_quick = 0; //计算快速排序的比较次数,并初始化为0

int main()
{
double t1,t2;
double t;
int i,n;
cout << "****************************************************************" << endl;
cout << "the comparation of average time between mergesort and quicksort" << endl;
cout << "****************************************************************" << endl;

cout << "please input your choice(0-2):" << endl;
cout << "0---quit..." << endl;
cout << "1---Generate random instance" << endl;
cout << "2---Input instance" << endl;

int choice;
cin >> choice;

switch(choice)
{
case(0):exit(0); break ;
case(1):
{
double mergetime = 0;
double quicktime = 0;

int num;
cout << endl;
cout << "please input the number of arrays to sort: " <<endl; //输入进行排序的数组个数
cin>>num;
//手动输入数据
cout<<"if manual input datas?(Y/N)";//是否手动输入数据
char R;
cin>>R;
if(R=='Y'||R=='y')
{
for(int d=0;d<num;d++)
{
cout << "the number of datas in Group " << d+1 << " is: ";
cin>>n;
for(int z=1;z<=n;z++)
{
cin>>a[z];
b[z]=a[z];
}

t1=clock();
count_merge = 0;
mergesort(b,1,n);
t2=clock();
t=t2-t1;
mergetime=mergetime+t; // 计算归并的总时间
//showsort(b,1,n); //显示归并排序后的结果
cout<<"mergesort spends: "<< t <<" ms"<<endl;
cout << "the comparision number in mergesort is: " << count_merge << endl;// mergesort的比较次数

t1=clock();
count_quick = 0;
quicksort(a,1,n);
t2=clock();
t=t2-t1;
quicktime=quicktime+t; //计算快排的总时间
//showsort(a,1,n); //显示快速排序后的结果
cout<<"quicksort spends: "<< t <<" ms"<<endl;
cout << "the comparision number in quicksort is:" << count_quick << endl; //quicksort的比较次数
cout<<endl;
}
cout << "the average time of mergesort: "<< (mergetime/num) << " ms" << endl;//求mergesort的平均时间
cout << "the average time of quicksort: "<< (quicktime/num) << " ms" << endl;//求quicksort的平均时间
cout << endl;
return 0;
}
//随机产生数据
srand((unsigned)time(NULL));
for(int j=0; j<num; j++)
{
// i = rand()%(int)(20)+1; //产生随机数,在1~20之间
n = 100000;
for(int k=0; k<n; k++)
{
b[k] = a[k] = rand()%(int)(100001);//产生n个数,在0-100000范围内
}
//cout<<"show the not sorted arrays:"<<endl;
// showsort(b,1,n); //显示未排序的数组
cout << "the " << j+1 << " group generates randomly " << n << " datas" <<endl;

t1 = clock();
count_merge = 0;
mergesort(b,1,n);
t2 = clock();
t = t2-t1;
mergetime = mergetime+t;
//cout << endl;
//showsort(b,1,n); //显示归并排序后的结果
cout << "mergesort spends: " << t << " ms" << endl; //mergesort花费的时间
cout << "the comparision number in mergesort is: " << count_merge << endl;// mergesort的比较次数

t1 = clock();
count_quick = 0;
quicksort(a,1,n);
t2 = clock();
t = t2-t1;
quicktime = quicktime+t;
//showsort(a,1,n); //显示快速排序后的结果
cout << "quicksort spends: " << t << " ms" << endl; //quicksort花费的时间
cout << "the comparision number in quicksort is:" << count_quick << endl; //quicksort的比较次数
cout << endl;
}
cout << "the average time of mergesort: "<< (mergetime/num) << " ms" << endl;//求mergesort的平均时间
cout << "the average time of quicksort: "<< (quicktime/num) << " ms" << endl;//求quicksort的平均时间
cout << endl;
return 0;
};break;

default:
break;
}

}

//合并A[low„mid]和A[mid+1„high]两个数组,并进行排序
void merge(int A[],int low,int mid,int high)
{
int p,q,k;
p = low;
q = mid+1;
k = low;

while( (p<=mid) && (q<=high) )
{
count_merge++;
if( A[p]<A[q] )
{
B[k]=A[p];
p++;
}
else
{
B[k]=A[q];
q++;
}
k++;
}
if( p==(mid+1) )
{
while( (k<=high) && (q<=high) )
B[k++]=A[q++];
}
else
{
while( (k<=high) && (p<=mid) )
B[k++]=A[p++];
}
for( int i=low; i<=high; i++)
A[i]=B[i];
}

//先将整个A数组分裂成若干个数组,然后再升序排列
void mergesort(int A[],int low,int high)
{
int mid;
if( low<high )
{
mid=(low+high)/2;
mergesort(A,low,mid);
mergesort(A,mid+1,high);
merge(A,low,mid,high);
}
}

//划分元素A[low]的新位置w
void split(int A[],int low,int high,int &w)
{
int i=low;
int x=A[low];
int temp;
for(int j=(low+1); j<=high; j++)
{
count_quick++;
if(A[j]<=x)
{
i++;
if(i!=j)
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
}

temp=A[low];
A[low]=A[i];
A[i]=temp;
w=i;
}

//按非降序排列的数组A中的元素
void quicksort(int A[],int low,int high)
{
int w;
if( low<high )
{
split(A,low,high,w);
quicksort(A,low,w-1);
quicksort(A,w+1,high);
}
}

//显示归并排序后的结果
void showsort(int A[],int low,int high)
{
for(int i=low; i<=high; i++)
cout<<A[i]<<" ";
cout<<endl;
}
运行结果:
...全文
855 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-04-01
  • 打赏
  • 举报
回复
各种排序算法动画演示http://www.webhek.com/misc/comparison-sort/
HeartFang 2017-04-01
  • 打赏
  • 举报
回复
引用 7 楼 paschen 的回复:
引用 5 楼 HeartFang 的回复:
[quote=引用 4 楼 paschen 的回复:] DEBUG下没有可比性
啥意思?编程小白不懂
前者是调试版,后者是优化版 其次还看数据,数据是否基本有序等均有定影响[/quote]这个我懂了,谢谢,但是为什么我的同学写的同一个功能的代码,他正在debug模式运行却不会出现我这种问题?
paschen 版主 2017-04-01
  • 打赏
  • 举报
回复
引用 5 楼 HeartFang 的回复:
引用 4 楼 paschen 的回复:
DEBUG下没有可比性
啥意思?编程小白不懂
前者是调试版,后者是优化版 其次还看数据,数据是否基本有序等均有定影响
xskxzr 2017-03-31
  • 打赏
  • 举报
回复

自己上网搜debug模式和release模式的区别。
HeartFang 2017-03-31
  • 打赏
  • 举报
回复
引用 4 楼 paschen 的回复:
DEBUG下没有可比性
啥意思?编程小白不懂
paschen 版主 2017-03-31
  • 打赏
  • 举报
回复
DEBUG下没有可比性
xskxzr 2017-03-31
  • 打赏
  • 举报
回复
用release模式
HeartFang 2017-03-31
  • 打赏
  • 举报
回复
如楼上。。第一次归并比快排慢,但第二次开始之后,归并就突然快了一半
HeartFang 2017-03-31
  • 打赏
  • 举报
回复

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧