武汉理工大学《数据结构》课程设计说明书
flag=1;
BC++; }
if(R[j]>=R[j-1]) BC++; } }
return BC; }
long quicksort(long R[],long left,long right) {
static long QC=0; long i=left,j=right; long temp=R[i]; while(i while((R[j]>temp)&&(j>i)) { QC++; j=j-1; } if(j>i) { R[i]=R[j]; i=i+1; QC++; } while((R[i]<=temp)&&(j>i)) { QC++; i=i+1; } if(i R[j]=R[i]; j=j-1; QC++; } } // R[i]=temp; if(left quicksort(R,left,i-1); // if(i+1 9 二次划分得到基准值的正确位置递归调用左子区间 武汉理工大学《数据结构》课程设计说明书 quicksort(R,i+1,right); //递归调用右子区间 return QC; } long quicksortcompare(long R[],long left,long right) { static long QC=0; long i=left,j=right; long temp=R[i]; while(i while((R[j]>temp)&&(j>i)) { QC++; j=j-1; } if(j>i) { R[i]=R[j]; i=i+1; } while((R[i]<=temp)&&(j>i)) { QC++; i=i+1; } if(i R[j]=R[i]; j=j-1; } } // R[i]=temp; if(left quicksort(R,left,i-1); if(i+1 quicksort(R,i+1,right); // return QC; } void operate1(long a[],long n) { long*R=new long[n]; 二次划分得到基准值的正确位置 //递归调用左子区间递归调用右子区间 10 武汉理工大学《数据结构》课程设计说明书 time_t start,end; double dif; long degree,compare; int i; for(i=0;i R[i]=a[i]; } time(&start); degree=Bubblesort(R,n); time(&end); dif=difftime(end,start); cout<<\冒泡排序交换次数\\t\ for(i=0;i R[i]=a[i]; } compare=Bubblesortcompare(R,n); cout<<\冒泡排序比较次数\\t\ } void operate2(long a[],long n) { long*R=new long[n]; time_t start,end; double dif; long degree,compare; int i; for(i=0;i R[i]=a[i]; } time(&start); degree=quicksort(R,0,n-1); time(&end); dif=difftime(end,start); cout<<\快速排序交换次数\\t\ for(i=0;i R[i]=a[i]; } compare=quicksortcompare(R,0,n-1); 11 武汉理工大学《数据结构》课程设计说明书 cout<<\快速排序比较次数\\t\ cout<<'\\n'; } void main(int argc, char* argv[]) { clock_t start_0, finish,start_1,finish_1; double duration; long n; char check; cout<<\排序算法比较 **\ cout<<\ while(1) { cout<<\请输入要产生随机数的个数\ long n; cin>>n; cout< long*a=new long[n]; srand((unsigned long)time(NULL)); for(long i=0;i a[i]=rand()%n; } start_0 = clock(); operate1(a,n); finish = clock(); cout<<'\\n'; duration = (double)(finish - start_0) / CLOCKS_PER_SEC; cout<<\程序运行花费时间: \秒\\n\\n\\n\start_1 = clock(); operate2(a,n); finish_1 = clock(); cout<<'\\n'; duration = (double)(finish_1 - start_1) / CLOCKS_PER_SEC; cout<<\程序运行花费时间: \秒\\n\\n\\n\ cout<<\是否重复实验?\\n继续请按y\\n退出请按n\\n请输入您的选择:\\t\\t\ cin>>check; if(check=='y'||check=='Y') continue; if(check=='n'||check=='N') break; } 12