目錄
創(chuàng)新互聯(lián)于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元城陽做網(wǎng)站,已為上家服務(wù),為城陽各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108排序綜合? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
一、問題描述
二、運(yùn)行環(huán)境說明?
三、代碼段?
四、效果展示?
備注:大二(上)數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)B題
一、問題描述給定N個(gè)(長整型范圍內(nèi)的)整數(shù),要求輸出從小到大排序后的結(jié)果
本題旨在測試各種排序算法在各種數(shù)據(jù)情況下的表現(xiàn)
各組測試數(shù)據(jù)特點(diǎn)如下:
· ?數(shù)據(jù)1:11個(gè)不相同的整數(shù);
· ?數(shù)據(jù)2:1000個(gè)隨機(jī)整數(shù);
· ?數(shù)據(jù)3:10000個(gè)隨機(jī)整數(shù);
· ?數(shù)據(jù)4:100000個(gè)隨機(jī)整數(shù);
· ?數(shù)據(jù)5:100000個(gè)順序整數(shù);
· ?數(shù)據(jù)6:100000個(gè)逆序整數(shù)
輸入格式:
第一行給出正整數(shù)N(N≤10?0000)
隨后一行給出N個(gè)(長整型范圍內(nèi)的)整數(shù),其間以空格分隔
輸出格式:
在一行中輸出從小到大排序后的結(jié)果,數(shù)字間以1個(gè)空格分隔
輸入樣例:
11
4 981 10 -17 0 -20 29 50 8 43 -5
輸出樣例:
-20 -17 -5 0 4 8 10 29 43 50 981
二、運(yùn)行環(huán)境說明?三、代碼段?【測試任務(wù)】
(1)按上述輸入樣例的格式生成數(shù)據(jù)1~數(shù)據(jù)6這6組數(shù)據(jù)集,
分別存儲(chǔ)到data1.txt ~?data6.txt這6個(gè)文件中
(2)實(shí)現(xiàn)以下6種排序算法:
? 直接插入排序、希爾排序、起泡排序、快速排序、簡單選擇排序、堆排序
(3)在主函數(shù)中分別調(diào)用上述排序函數(shù),對(duì)data1.txt ~?data6.txt文件中的數(shù)據(jù)進(jìn)行排序,
? 并記錄不同排序算法對(duì)不同數(shù)據(jù)集進(jìn)行排序的耗時(shí),將耗時(shí)輸出到顯示器,
? 并將排序結(jié)果按輸出樣例格式輸出到result1.txt ~ result6.txt這6個(gè)文件中
#include#include
#includeusing namespace std;
//--------------------------------------------------------------------------------------------
void InsertSort(int arr[], int length) //直接插入排序
{
if (arr == nullptr || length<= 0)
return;
for(int i=2;i<=length;i++)
if(arr[i]=1 && arr[0]=1 && arr[0]0 && flag)
{
flag=false;
for(int j=1;j<=m;j++)
if(arr[j]>arr[j+1])
{
flag=true;
int tem=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tem;
}
m--;
}
}
void QuickSort(int q[],int l,int r) //快速排序
{
if(l>=r) return;
int i=l-1,j=r+1,x=q[l+r>>1];
while(ix);
if(i0;i--)
HeapAdjust(arr,i,n);
}
void HeapSort(int arr[], int length) //堆排序
{
CreatHeap(arr,length);
for(int i=length;i>1;i--)
{
int tem=arr[1];
arr[1]=arr[i];
arr[i]=tem;
HeapAdjust(arr,1,i-1);
}
}
//--------------------------------------------------------------------------------------------
void GetData() //獲取整數(shù)數(shù)據(jù)函數(shù)
{
FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6; //文件指針
fp1 = fopen("data1.txt", "w");
fprintf(fp1,"11\n");
fclose(fp1);
fp1 = fopen("data1.txt", "a");
fprintf(fp1,"4 981 10 -17 0 -20 29 50 8 43 -5");
fclose(fp1);
fp2 = fopen("data2.txt", "w");
fprintf(fp2,"1000\n");
fclose(fp2);
fp2 = fopen("data2.txt", "a");
srand((int)time(0)); //隨機(jī)種子
for(int i=1;i<=1000;i++)
fprintf(fp2,"%d ",((rand()<<15)+rand())%10000); //隨機(jī)整數(shù)的范圍為[0,10000)
fclose(fp2);
fp3 = fopen("data3.txt", "w");
fprintf(fp3,"10000\n");
fclose(fp3);
fp3 = fopen("data3.txt", "a");
srand((int)time(0)); //隨機(jī)種子
for(int i=1;i<=10000;i++)
fprintf(fp3,"%d ",((rand()<<15)+rand())%100000); //隨機(jī)整數(shù)的范圍為[0,100000)
fclose(fp3);
fp4 = fopen("data4.txt", "w");
fprintf(fp4,"100000\n");
fclose(fp4);
fp4 = fopen("data4.txt", "a");
srand((int)time(0)); //隨機(jī)種子
for(int i=1;i<=100000;i++)
fprintf(fp4,"%d ",((rand()<<15)+rand())%1000000); //隨機(jī)整數(shù)的范圍為[0,1000000)
fclose(fp4);
fp5 = fopen("data5.txt", "w");
fprintf(fp5,"100000\n");
fclose(fp5);
fp5 = fopen("data5.txt", "a");
for(int i=1;i<=100000;i++)
fprintf(fp5,"%d ",i); //100000個(gè)順序整數(shù)
fclose(fp5);
fp6 = fopen("data6.txt", "w");
fprintf(fp6,"100000\n");
fclose(fp6);
fp6 = fopen("data6.txt", "a");
for(int i=100000;i>=1;i--)
fprintf(fp6,"%d ",i); //100000個(gè)逆序整數(shù)
fclose(fp6);
}
//--------------------------------------------------------------------------------------------
void InitData(int R1[],int R2[],int R3[],int R4[],int R5[],int R6[]) //讀取整數(shù)數(shù)據(jù)函數(shù)
{
FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6; //文件指針
if((fp1 = fopen("data1.txt", "r")) == NULL)
cout<< "不能打開文件data1.txt"<< endl;
else
{
int count1=0;
while(!feof(fp1))
{
fscanf(fp1,"%d",&R1[count1]);
count1++;
}
}
fclose(fp1);
if((fp2 = fopen("data2.txt", "r")) == NULL)
cout<< "不能打開文件data2.txt"<< endl;
else
{
int count2=0;
while(!feof(fp2))
{
fscanf(fp2,"%d",&R2[count2]);
count2++;
}
}
fclose(fp2);
if((fp3 = fopen("data3.txt", "r")) == NULL)
cout<< "不能打開文件data3.txt"<< endl;
else
{
int count3=0;
while(!feof(fp3))
{
fscanf(fp3,"%d",&R3[count3]);
count3++;
}
}
fclose(fp3);
if((fp4 = fopen("data4.txt", "r")) == NULL)
cout<< "不能打開文件data4.txt"<< endl;
else
{
int count4=0;
while(!feof(fp4))
{
fscanf(fp4,"%d",&R4[count4]);
count4++;
}
}
fclose(fp4);
if((fp5 = fopen("data5.txt", "r")) == NULL)
cout<< "不能打開文件data5.txt"<< endl;
else
{
int count5=0;
while(!feof(fp5))
{
fscanf(fp5,"%d",&R5[count5]);
count5++;
}
}
fclose(fp5);
if((fp6 = fopen("data6.txt", "r")) == NULL)
cout<< "不能打開文件data6.txt"<< endl;
else
{
int count6=0;
while(!feof(fp6))
{
fscanf(fp6,"%d",&R6[count6]);
count6++;
}
}
fclose(fp6);
}
//--------------------------------------------------------------------------------------------
double VariousSort(char c, int R[], int length)
{
double t;
clock_t startTime,endTime;
if(c=='I')//直接插入排序
{
startTime = clock(); InsertSort(R,length); endTime = clock();
t = (double)(endTime - startTime)/CLOCKS_PER_SEC;
return t;
}
else if(c=='M')//希爾排序
{
int len=3;
int dt[]={5,3,1};
startTime = clock();
MainShellSort(R,length,dt,len);
endTime = clock();
t = (double)(endTime - startTime)/CLOCKS_PER_SEC;
return t;
}
else if(c=='B')//冒泡排序
{
startTime = clock(); BubbleSort(R,length); endTime = clock();
t = (double)(endTime - startTime)/CLOCKS_PER_SEC;
return t;
}
else if(c=='Q')//快速排序
{
startTime = clock(); QuickSort(R,1,length); endTime = clock();
t = (double)(endTime - startTime)/CLOCKS_PER_SEC;
return t;
}
else if(c=='S')//簡單選擇排序
{
startTime = clock(); SelectSort(R,length); endTime = clock();
t = (double)(endTime - startTime)/CLOCKS_PER_SEC;
return t;
}
else//堆排序
{
startTime = clock(); HeapSort(R,length); endTime = clock();
t = (double)(endTime - startTime)/CLOCKS_PER_SEC;
return t;
}
}
//--------------------------------------------------------------------------------------------
void GetResult(int R1[],int R2[],int R3[],int R4[],int R5[],int R6[]) //獲取排序結(jié)果函數(shù)
{
double t[10];
InitData(R1,R2,R3,R4,R5,R6);
t[1]=VariousSort('I', R1, 11);
t[2]=VariousSort('I', R2, 1000);
t[3]=VariousSort('I', R3, 10000);
t[4]=VariousSort('I', R4, 100000);
t[5]=VariousSort('I', R5, 100000);
t[6]=VariousSort('I', R6, 100000);
printf("直接插入排序:\n");
for(int i=1;i<=6;i++)
printf("排序數(shù)據(jù)%d用時(shí)%lf秒\n",i,t[i]);
printf("對(duì)六類數(shù)據(jù)排序所用總時(shí)間為%lf秒\n",t[1]+t[2]+t[3]+t[4]+t[5]+t[6]);
puts("");
InitData(R1,R2,R3,R4,R5,R6);
t[1]=VariousSort('M', R1, 11);
t[2]=VariousSort('M', R2, 1000);
t[3]=VariousSort('M', R3, 10000);
t[4]=VariousSort('M', R4, 100000);
t[5]=VariousSort('M', R5, 100000);
t[6]=VariousSort('M', R6, 100000);
printf("希爾排序:\n");
for(int i=1;i<=6;i++)
printf("排序數(shù)據(jù)%d用時(shí)%lf秒\n",i,t[i]);
printf("對(duì)六類數(shù)據(jù)排序所用總時(shí)間為%lf秒\n",t[1]+t[2]+t[3]+t[4]+t[5]+t[6]);
puts("");
InitData(R1,R2,R3,R4,R5,R6);
t[1]=VariousSort('B', R1, 11);
t[2]=VariousSort('B', R2, 1000);
t[3]=VariousSort('B', R3, 10000);
t[4]=VariousSort('B', R4, 100000);
t[5]=VariousSort('B', R5, 100000);
t[6]=VariousSort('B', R6, 100000);
printf("冒泡排序:\n");
for(int i=1;i<=6;i++)
printf("排序數(shù)據(jù)%d用時(shí)%lf秒\n",i,t[i]);
printf("對(duì)六類數(shù)據(jù)排序所用總時(shí)間為%lf秒\n",t[1]+t[2]+t[3]+t[4]+t[5]+t[6]);
puts("");
InitData(R1,R2,R3,R4,R5,R6);
t[1]=VariousSort('Q', R1, 11);
t[2]=VariousSort('Q', R2, 1000);
t[3]=VariousSort('Q', R3, 10000);
t[4]=VariousSort('Q', R4, 100000);
t[5]=VariousSort('Q', R5, 100000);
t[6]=VariousSort('Q', R6, 100000);
printf("快速排序:\n");
for(int i=1;i<=6;i++)
printf("排序數(shù)據(jù)%d用時(shí)%lf秒\n",i,t[i]);
printf("對(duì)六類數(shù)據(jù)排序所用總時(shí)間為%lf秒\n",t[1]+t[2]+t[3]+t[4]+t[5]+t[6]);
puts("");
InitData(R1,R2,R3,R4,R5,R6);
t[1]=VariousSort('S', R1, 11);
t[2]=VariousSort('S', R2, 1000);
t[3]=VariousSort('S', R3, 10000);
t[4]=VariousSort('S', R4, 100000);
t[5]=VariousSort('S', R5, 100000);
t[6]=VariousSort('S', R6, 100000);
printf("簡單選擇排序:\n");
for(int i=1;i<=6;i++)
printf("排序數(shù)據(jù)%d用時(shí)%lf秒\n",i,t[i]);
printf("對(duì)六類數(shù)據(jù)排序所用總時(shí)間為%lf秒\n",t[1]+t[2]+t[3]+t[4]+t[5]+t[6]);
puts("");
InitData(R1,R2,R3,R4,R5,R6);
t[1]=VariousSort('H', R1, 11);
t[2]=VariousSort('H', R2, 1000);
t[3]=VariousSort('H', R3, 10000);
t[4]=VariousSort('H', R4, 100000);
t[5]=VariousSort('H', R5, 100000);
t[6]=VariousSort('H', R6, 100000);
printf("堆排序:\n");
for(int i=1;i<=6;i++)
printf("排序數(shù)據(jù)%d用時(shí)%lf秒\n",i,t[i]);
printf("對(duì)六類數(shù)據(jù)排序所用總時(shí)間為%lf秒\n",t[1]+t[2]+t[3]+t[4]+t[5]+t[6]);
FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6; //文件指針
fp1 = fopen("result1.txt", "w");
for(int i=1;i<=11;i++)
fprintf(fp1,"%d ",R1[i]);
fclose(fp1);
fp2 = fopen("result2.txt", "w");
for(int i=1;i<=1000;i++)
fprintf(fp2,"%d ",R2[i]);
fclose(fp2);
fp3 = fopen("result3.txt", "w");
for(int i=1;i<=10000;i++)
fprintf(fp3,"%d ",R3[i]);
fclose(fp3);
fp4 = fopen("result4.txt", "w");
for(int i=1;i<=100000;i++)
fprintf(fp4,"%d ",R4[i]);
fclose(fp4);
fp5 = fopen("result5.txt", "w");
for(int i=1;i<=100000;i++)
fprintf(fp5,"%d ",R5[i]);
fclose(fp5);
fp6 = fopen("result6.txt", "w");
for(int i=1;i<=100000;i++)
fprintf(fp6,"%d ",R6[i]);
fclose(fp6);
}
//--------------------------------------------------------------------------------------------
int main()
{
int R1[20],R2[1010],R3[10010],R4[100010],R5[100010],R6[100010];
GetData();
printf("六類數(shù)據(jù)預(yù)覽:\n");
printf("數(shù)據(jù)1:11個(gè)不相同的整數(shù)\n");
printf("數(shù)據(jù)2:1,000個(gè)[0,10000)范圍內(nèi)隨機(jī)整數(shù)\n");
printf("數(shù)據(jù)3:10,000個(gè)[0,100000)范圍內(nèi)隨機(jī)整數(shù)\n");
printf("數(shù)據(jù)4:100,000個(gè)[0,1000000)范圍內(nèi)隨機(jī)整數(shù)\n");
printf("數(shù)據(jù)5:[1,100000]范圍內(nèi)所有整數(shù)從小到大排列\(zhòng)n");
printf("數(shù)據(jù)6:[1,100000]范圍內(nèi)所有整數(shù)從大到小排列\(zhòng)n\n");
printf("接下來將調(diào)用六類排序方法對(duì)以上六類數(shù)據(jù)進(jìn)行從小到大排序,并測算排序所用時(shí)間!\n\n");
GetResult(R1,R2,R3,R4,R5,R6);
return 0;
}
四、效果展示?你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧