



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)一 算法的時間復(fù)雜度一、實(shí)驗(yàn)?zāi)康呐c要求熟悉C/C+語言的集成開發(fā)環(huán)境; 通過本實(shí)驗(yàn)加深對算法分析基礎(chǔ)知識的理解。二、實(shí)驗(yàn)內(nèi)容 : 掌握算法分析的基本方法,并結(jié)合具體的問題深入認(rèn)識算法的時間復(fù)雜度分析。三、實(shí)驗(yàn)題定義一個足夠大的整型數(shù)組, 并分別用起泡排序、 簡單選擇排序、 快速排序和歸并排序?qū)?shù) 組中的數(shù)據(jù)進(jìn)行排序(按從小到大的順序排序) ,記錄每種算法的實(shí)際耗時,并結(jié)合數(shù)據(jù)結(jié) 構(gòu)中的知識對算法的時間復(fù)雜度分析進(jìn)行說明。實(shí)驗(yàn)數(shù)據(jù)分兩種情況:1、數(shù)組中的數(shù)據(jù)隨機(jī)生成;2、數(shù)組中的數(shù)據(jù)已經(jīng)是非遞減有序。四、實(shí)驗(yàn)步驟 理解算法思想和問題要求; 編程實(shí)現(xiàn)題目要求; 上機(jī)輸入和調(diào)試自己所編的程序;
2、驗(yàn)證分析實(shí)驗(yàn)結(jié)果; 整理出實(shí)驗(yàn)報告。五、實(shí)驗(yàn)程序 #include<iostream> #include<time.h> #include<windows.h> using namespace std;void SelectSort(int r , int n)int i;int j;int index;int temp;for (i=0; i<n -1; i+)index=i;for (j=i+1; j<n; j+)if (rj<rindex) index=j;if (index!=i)temp=ri; ri=rindex; rindex
3、=temp;for(i=0;i<n;i+)cout<<ri<<" " cout<<"n"void BubbleSort(int r, int n)int temp;int exchange;int bound;exchange=n-1;while (exchange)bound=exchange; exchange=0;for (int j=0; j<bound; j+) if (rj>rj+1)temp=rj;rj=rj+1;rj+1=temp;exchange=j;for(int i=0;i<
4、;n;i+) cout<<ri<<" "cout<<"n"int Partition(int r, int first, int end)int i=first;int j=end;int temp;while (i<j)while (i<j && ri<= rj)j-;if (i<j)temp=ri;ri=rj; rj=temp;i+;while (i<j && ri<= rj) i+; if (i<j) temp=rj; rj=ri; ri=
5、temp;j-;return i;/ 快速排序void QuickSort(int r, int first, int end) if (first<end)int pivot=Partition(r, first, end);QuickSort(r, first, pivot -1);QuickSort(r, pivot+1, end);void Merge(int r, int r1, int s, int m, int t) int i=s;int j=m+1;int k=s;while (i<=m && j<=t)if (ri<=rj)r1k+=
6、ri+;elser1k+=rj+;if (i<=m)while (i<=m)r1k+=ri+;elsewhile (j<=t)r1k+=rj+;void MergePass(int r , int r1 , int n, int h)int i=0;int k;while (i<=n -2*h)Merge(r, r1, i, i+h -1, i+2*h -1); i+=2*h;if (i<n -h)Merge(r, r1, i, i+h -1, n);else for (k=i; k<=n; k+)r1k=rk;void MergeSort2(int r,
7、int r1, int r2,int s, int t) int m;if (s=t)r1s=rs;elsem=(s+t)/2;MergeSort2(r, r2, r1, s, m);MergeSort2(r, r2, r1, m+1, t); Merge(r2, r1, s, m, t);int main()int b100;const int numv=100; clock_t t=clock();for(int k=0;k<100;k+) bk=rand()%100;cout<<bk<<" "cout << "n 起
8、泡排序結(jié)果為: " << "n"BubbleSort(b, numv);cout<<clock() -t<<"ms"<<endl;cout<<"n 簡單選擇排序結(jié)果為: "<<"n"SelectSort(b, numv);cout<<clock() -t<<"ms"<<endl; cout<<"n 快速排序結(jié)果為: "<<"n
9、" QuickSort(b,0,100);for(int j=0;j<100;j+) cout<<bj<<" "cout<<"n"cout<<clock() -t<<"ms"<<endl;const int h=100;int b1h;for(int i=0;i<h;i+) b1i=rand()%k;int a1h;int c1h;cout<<"n 歸并排序結(jié)果為: "<<"n" MergeSort2(b1,a1,c1,0,h -1);for(int m=0; m < h; m+) cout<<a1m<<" "cout<<"n"cout<<(clock() -t)<<"ms"<<endl;return 0;六 實(shí)驗(yàn)結(jié)果 當(dāng)隨機(jī)數(shù)為 10 時: 起泡排序結(jié)果為: 4ms 簡單選擇排序結(jié)果為: 7ms 快速排序結(jié)果為: 12ms 歸并排序結(jié)果為: 16ms 當(dāng)隨機(jī)數(shù)為 100 時: 起泡
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- D打印技術(shù)在個性化教育資源的開發(fā)考核試卷
- 期刊出版論文的開源出版趨勢考核試卷
- 教育音像制品策劃與制作考核試卷
- 文具行業(yè)個性化服務(wù)考核試卷
- 工業(yè)園區(qū)電動汽車充電需求分析考核試卷
- 健康生活方式與營養(yǎng)健康考核試卷
- 個人培訓(xùn)課件大全
- 買杭州新房合同范本
- 私人店鋪?zhàn)赓U合同范本
- 2025屆吉林省吉林地區(qū)高三上學(xué)期二模英語試題及答案
- GB/T 15934-2008電器附件電線組件和互連電線組件
- GA/T 765-2020人血紅蛋白檢測金標(biāo)試劑條法
- 第2章-西周-春秋戰(zhàn)國時期的音樂-1-3節(jié)課件
- 提高白云石配比對燒結(jié)生產(chǎn)的影響
- 公安基礎(chǔ)知識考試題庫(含各題型)
- 選礦試車方案
- 小課題專題研究參考題目
- 《最好的未來》合唱曲譜
- GB∕T 8081-2018 天然生膠 技術(shù)分級橡膠(TSR)規(guī)格導(dǎo)則
- 教學(xué)課件個人理財(cái)-2
- 航空航天概論(課堂PPT)
評論
0/150
提交評論