do{j--;}while(A[j]>A[left]);
if(i{
Swap(A[i],A[j]);
}
}while(iSwap(A[left],A[j]);
QSort_2(A,left,j-1);
QSort_2(A,j+1,right);
}
else
{
InsertSort_2(A,left,right);
}
}
}
template
voidQuickSort_2(TA[],intn)//改进后的快速排序
{
QSort_2(A,0,n-1);
}
template
voidMerge(TA[],inti1,intj1,inti2,intj2)//两路合并排序
{
T*Temp=newT[j2-i1+1];
inti=i1,j=i2,k=0;
while(i<=j1&&j<=j2)
{
if(A[i]<=A[j])
{
Temp[k++]=A[i++];
}
else
{
Temp[k++]=A[j++];
}
}
while(i<=j1)
{
Temp[k++]=A[i++];
}
while(j<=j2)
{
Temp[k++]=A[j++];
}
for(i=0;i{
A[i1++]=Temp[i];
}
delete[]Temp;
}
template
voidMergeSort(TA[],intn)
{
inti1,j1,i2,j2;
intsize=1;
while(size{
i1=0;
while(i1+size{
i2=i1+size;
j1=i2-1;
if(i2+size-1>n-1)
{
j2=n-1;
}
else
{
j2=i2+size-1;
}
Merge(A,i1,j1,i2,j2);
i1=j2+1;
}
size*=2;
}
}
template
voidAdjustDown(TA[],intr,intj)
{
intchild=2*r+1;
Ttemp=A[r];
while(child<=j)
{
if((child{
child++;
}
if(temp>=A[child])
{
break;
}
A[(child-1)/2]=A[child];
child=2*child+1;
}
A[(child-1)/2]=temp;
}
template
voidHeapSort(TA[],intn)//堆排序
{
for(inti=(n-2)/2;i>-1;i--)
{
AdjustDown(A,i,n-1);
}
for(inti=n-1;i>0;i--)
{
Swap(A[0],A[i]);
AdjustDown(A,0,i-1);
}
}
template
T*Produce(intchoose)//生成数组(顺序、逆序、随机)
{
T*p=newT[N];
switch(choose)
{
case1:
for(inti=0;i{
p[i]=i;
}break;
case2:
for(inti=0;i{
p[i]=N-1-i;
}break;
case3:
srand(time(NULL));
for(inti=0;i{
p[i]=rand()%N;
}break;
default:
cout<<"error!
"<}
returnp;
}
template
doubleSelectSort_time(intchoose)//计算简单选择排序的时间
{
T*A;
srand(time(NULL));
doubletime;
clock_tstart,finish;
start=clock();
for(inti=0;i<100;i++)
{
A=Produce(choose);
SelectSort(A,N);
delete[]A;
}
finish=clock();
time=(double)(finish-start)/CLOCKS_PER_SEC;
returntime;
}
template
doubleInsertSort_time(intchoose)//计算直接插入排序的时间
{
T*A;
doubletime;
clock_tstart,finish;
start=clock();
for(inti=0;i<100;i++)
{
A=Produce(choose);
InsertSort(A,N);
delete[]A;
}
finish=clock();
time=(double)(finish-start)/CLOCKS_PER_SEC;
returntime;
}
template
doubleBubbleSort_time(intchoose)//计算冒泡排序的时间
{
T*A;
doubletime;
clock_tstart,finish;
start=clock();
for(inti=0;i<100;i++)
{
A=Produce(choose);
BubbleSort(A,N);
delete[]A;
}
finish=clock();
time=(double)(finish-start)/CLOCKS_PER_SEC;
returntime;
}
template
doubleQuickSort_time(intchoose)//计算快速排序的时间
{
T*A;
doubletime;
clock_tstart,finish;
start=clock();
for(inti=0;i<100;i++)
{
A=Produce(choose);
QuickSort(A,N);
delete[]A;
}
finish=clock();
time=(double)(finish-start)/CLOCKS_PER_SEC;
returntime;
}
template
doubleQuickSort_2_time(intchoose)//计算改进后的快速排序的时间
{
T*A;
doubletime;
clock_tstart,finish;
start=clock();
for(inti=0;i<100;i++)
{
A=Produce(choose);
QuickSort_2(A,N);
delete[]A;
}
finish=clock();
time=(double)(finish-start)/CLOCKS_PER_SEC;
returntime;
}
template
doubleMergeSort_time(intchoose)//计算两路合并排序的时间
{
T*A;
doubletime;
clock_tstart,finish;
start=clock();
for(inti=0;i<100;i++)
{
A=Produce(choose);
MergeSort(A,N);
delete[]A;
}
finish=clock();
time=(double)(finish-start)/CLOCKS_PER_SEC;
returntime;
}
template
doubleHeapSort_time(intchoose)//计算堆排序的时间
{
T*A;
doubletime;
clock_tstart,finish;
start=clock();
for(inti=0;i<100;i++)
{
A=Produce(choose);
HeapSort(A,N);
delete[]A;
}
finish=clock();
time=(double)(finish-start)/CLOCKS_PER_SEC;
returntime;
}
template
voidPrint(T*A)//输出数组的函数
{
for(inti=0;i{
cout<}
cout<}
voidmenu_1()
{
cout<<"欢迎使用内排序算法的实现及性能比较系统!
"<cout<<"1、简单选择排序"<cout<<"2、直接插入排序"<cout<<"3、冒泡排序"<cout<<"4、快速排序"<cout<<"5、改进后的快速排序"<cout<<"6、两路合并排序"<cout<<"7、堆排序"<}
voidmenu_2()
{
cout<<"您想检验什么?
"<cout<<"1、各个排序是否可行"<";
}
voidchildmenu_1()
{
cout<<"1、最好的情况(顺序数组)"<cout<<"2、最差的情况(逆序数组)"<cout<<"3、平均的情况(随机数组)"<cout<";
}
intmain()
{
charflag='Y';
while(flag=='Y'||flag=='y')
{
system("cls");
menu_1();
intchoose;
menu_2();
cin>>choose;
if(choose==1)
{
intnumber;
cout<";
cin>>number;
int*A=Produce(3);
cout<<"排序前:
";
Print(A);
switch(number)
{
case1:
SelectSort(A,N);break;
case2:
InsertSort(A,N);break;
case3:
BubbleSort(A,N);break;
case4:
QuickSort(A,N);break;
case5:
QuickSort_2(A,N);break;
case6:
MergeSort(A,N);break;
case7:
HeapSort(A,N);break;
}
cout<";
Print(A);
}
elseif(choose==2)
{
system("cls");
menu_1();
intfirst,second;
doubletime;
cout<<"请输入想要检验的排序的编号:
";
cin>>first;
cout<childmenu_1();
cin>>second;
switch(first)
{
case1:
time=SelectSort_time(second);
cout<<"用时:
"<