C語(yǔ)言四種排序算法時(shí)間復(fù)雜度比較_第1頁(yè)
C語(yǔ)言四種排序算法時(shí)間復(fù)雜度比較_第2頁(yè)
C語(yǔ)言四種排序算法時(shí)間復(fù)雜度比較_第3頁(yè)
C語(yǔ)言四種排序算法時(shí)間復(fù)雜度比較_第4頁(yè)
C語(yǔ)言四種排序算法時(shí)間復(fù)雜度比較_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1、方案設(shè)計(jì): 我這次實(shí)驗(yàn)通過(guò)隨機(jī)生成30000個(gè)隨機(jī)數(shù),把隨機(jī)數(shù)存到數(shù)組中,用這同一組隨機(jī)數(shù)據(jù)分別進(jìn)行四種排序,直接插入排序、直接選擇排序、冒泡排序和快速排序。還通過(guò)了調(diào)用txt文件把運(yùn)算所需時(shí)間導(dǎo)出,分別輸出各個(gè)算法所需用時(shí)并對(duì)用時(shí)時(shí)長(zhǎng)再進(jìn)行冒泡排序算出用時(shí)最短的算法。2、程序代碼:#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <windows.h>#include <time.h>#define N 30000void Wrong() /輸入錯(cuò)誤

2、printf("n語(yǔ)法錯(cuò)誤,請(qǐng)重新輸入!n"); getchar();void Disp(int a) /清屏 int i; system("cls"); for(i=0; i<N; i+) if(i-1)%10=9) printf("n"); printf("%-7d",ai); void InsertSort(int a,int p) /直接插入排序算法 int i,j,temp; for(i=1; i<N; i+) temp=ai; for(j=i; j>0&&aj-1>

3、;temp; j-) aj=aj-1; aj=temp; void SelectSort(int a,int p) /選擇排序算法 int i,j,k; for(i=0; i<N-1; i+) k=i; for(j=i+1; j<N; j+) if(aj<ak) k=j; if(k!=i) int temp; temp=ak; ak=ai; ai=temp; void BubbleSort(int a,int p) /冒泡排序算法 int i,j,temp; for (i=0; i<N-1; i+) for (j=N-1; j>i; j-) /比較,找出本趟最小關(guān)

4、鍵字的記錄 if (aj<aj-1) temp=aj; /進(jìn)行交換,將最小關(guān)鍵字記錄前移 aj=aj-1; aj-1=temp; void quicksort(int a,int n,int p) /快速排序算法 int i,j,low,high,temp,top=-1; struct node int low,high; stN; top+; sttop.low=0; sttop.high=n-1; while(top>-1) low=sttop.low; high=sttop.high; top-; i=low; j=high; if(low<high) temp=alo

5、w; while(i!=j) while(i<j&&aj>temp)j-; if(i<j) ai=aj; i+; while(i<j&&ai<temp)i+; if(i<j) aj=ai; j-; ai=temp; top+; sttop.low=low; sttop.high=i-1; top+; sttop.low=i+1; sttop.high=high; double TInsertSort(int a,int p)/計(jì)算直接插入排序算法用時(shí) int i; int bN; for(i=0; i<N; i+) bi

6、=ai; LARGE_INTEGER m_liPerfFreq= 0; QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart= 0; QueryPerformanceCounter(&m_liPerfStart); InsertSort(b,p); LARGE_INTEGER liPerfNow= 0; QueryPerformanceCounter(&liPerfNow); double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart

7、; time/=m_liPerfFreq.QuadPart; if(p!=6) Disp(b); getchar(); printf("n用直接插入排序法用的時(shí)間為%f秒;",time); FILE *fp; fp=fopen("直接插入排序.txt","w"); for(i=0; i<N; i+) fprintf(fp,"%d ",bi); fclose(fp); return(time);double TSelectSort(int a,int p)/計(jì)算選擇排序用時(shí) int i; int bN; for

8、(i=0; i<N; i+) bi=ai; LARGE_INTEGER m_liPerfFreq= 0; QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart= 0; QueryPerformanceCounter(&m_liPerfStart); SelectSort(b,p); if(p!=6) Disp(b); getchar(); LARGE_INTEGER liPerfNow= 0; QueryPerformanceCounter(&liPerfNow); double

9、 time=liPerfNow.QuadPart - m_liPerfStart.QuadPart; time/=m_liPerfFreq.QuadPart; printf("n用直接選擇排序法用的時(shí)間為%f秒;",time); FILE *fp; fp=fopen("直接選擇排序.txt","w"); for(i=0; i<N; i+) fprintf(fp,"%d ",bi); fclose(fp); return(time);double TBubbleSort(int a,int p)/計(jì)算冒泡排序算

10、法用時(shí) int i; int bN; for(i=0; i<N; i+) bi=ai; LARGE_INTEGER m_liPerfFreq= 0; QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart= 0; QueryPerformanceCounter(&m_liPerfStart); BubbleSort(b,p); LARGE_INTEGER liPerfNow= 0; QueryPerformanceCounter(&liPerfNow); double time=l

11、iPerfNow.QuadPart - m_liPerfStart.QuadPart; time/=m_liPerfFreq.QuadPart; if(p!=6) Disp(b); getchar(); printf("n用冒泡排序法用的時(shí)間為%f秒;",time); FILE *fp; fp=fopen("冒泡排序.txt","w"); for(i=0; i<N; i+) fprintf(fp,"%d ",bi); fclose(fp); return(time);double Tquicksort(int

12、 a,int n,int p)/計(jì)算快速排序算法用時(shí) int i; int bN; for(i=0; i<N; i+) bi=ai; LARGE_INTEGER m_liPerfFreq= 0; QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart= 0; QueryPerformanceCounter(&m_liPerfStart); quicksort(b,N,p); LARGE_INTEGER liPerfNow= 0; QueryPerformanceCounter(&l

13、iPerfNow); double time=liPerfNow.QuadPart - m_liPerfStart.QuadPart; time/=m_liPerfFreq.QuadPart; if(p!=6) Disp(b); getchar(); printf("n用快速排序法用的時(shí)間為%f秒;",time); FILE *fp; fp=fopen("快速排序.txt","w"); for(i=0; i<N; i+) fprintf(fp,"%d ",bi); fclose(fp); return(tim

14、e);void BubleSort(double a) /時(shí)間數(shù)組的冒泡排序 int i,j; double temp; for(i=1; i<6; i+) for(j=4; j>=i; j-) if(aj+1<aj) temp=aj+1; aj+1=aj; aj=temp; void menu() printf("*nn"); printf("(1)顯示隨機(jī)數(shù)n"); printf("(2)直接插入排序n"); printf("(3)直接選擇排序n"); printf("(4)冒泡排序

15、n"); printf("(5)快速排序n"); printf("(6)時(shí)間效率比較n"); printf("n請(qǐng)?jiān)谏鲜鲂蛱?hào)中選擇一個(gè)并輸入:n"); printf("*n");void main() int i,p,aN; srand(int)time(NULL); /隨機(jī)種子 for(i=0; i<N; i+) ai=rand()%50000+1; while(1) system("cls"); menu(); scanf("%d",&p); if

16、(p=0) printf("謝謝使用!n"); getchar(); break; double TIMES5,TIMES15;/時(shí)間數(shù)組 switch(p) case 1: Disp(a); FILE *fp; fp=fopen("隨機(jī)數(shù).txt","w"); for(i=0; i<N; i+)fprintf(fp,"%d ",ai); fclose(fp); getchar(); printf("n請(qǐng)按任意鍵繼續(xù)!"); getchar(); break; case 2: TInser

17、tSort(a,p); printf("n請(qǐng)按任意鍵繼續(xù)!"); getchar(); break; case 3: TSelectSort(a,p); printf("n請(qǐng)按任意鍵繼續(xù)!"); getchar(); break; case 4: TBubbleSort(a,p); printf("n請(qǐng)按任意鍵繼續(xù)!"); getchar(); break; case 5: Tquicksort(a,N,p); printf("n請(qǐng)按任意鍵繼續(xù)!"); getchar(); break; case 6: syste

18、m("cls"); TIMES11=TIMES1=TInsertSort(a,p); TIMES12=TIMES2=TSelectSort(a,p); TIMES13=TIMES3=TBubbleSort(a,p); TIMES14=TIMES4=Tquicksort(a,N,p); getchar(); BubleSort(TIMES); printf("nn"); printf("排序這組數(shù)據(jù)較快的排序法是:n"); if(TIMES1=TIMES11) printf("直接插入排序:%f秒!n",TIMES1); if(TIMES1=TIMES12) printf("直接選擇排序:%f秒!n",TIMES1); if(TIMES1=TIMES13) printf("冒泡排序:%f秒!n&quo

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論