中國科技大學(xué)算法導(dǎo)論_第一次實驗報告_第1頁
中國科技大學(xué)算法導(dǎo)論_第一次實驗報告_第2頁
中國科技大學(xué)算法導(dǎo)論_第一次實驗報告_第3頁
中國科技大學(xué)算法導(dǎo)論_第一次實驗報告_第4頁
中國科技大學(xué)算法導(dǎo)論_第一次實驗報告_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、快速排序?qū)嶒瀳蟾鍿A14225010 1、 題目當(dāng)輸入數(shù)據(jù)已經(jīng)“幾乎”有序時,插入排序速度很快。在實際應(yīng)用中,我們可以利用這一特點來提高快速排序的速度。當(dāng)對一個長度小于k的子數(shù)組調(diào)用快速排序時,讓它不做任何排序就返回。當(dāng)上層的快速排序調(diào)用返回后,對整個數(shù)組運行插入排序來完成排序過程。試證明:這一排序算法的期望時間復(fù)雜度為O(nk+nlg(n/k)。分別從理論和實踐的角度說明我們應(yīng)該如何選擇k?2、 算法思想當(dāng)輸入數(shù)據(jù)已經(jīng)“幾乎”有序時,插入排序速度很快。當(dāng)對一個長度小于k的子數(shù)組調(diào)用快速排序時,讓它不做任何排序就返回。當(dāng)上層的快速排序調(diào)用返回后,對整個數(shù)組運行插入排序來完成排序過程。累加k的值

2、,計算出當(dāng)k為不同值時算法運行的時間,來算出當(dāng)k大約為什么值時運行的時間最短,并與傳統(tǒng)的快速排序算法的運行時間進行比較。3、 實驗結(jié)果輸入100個不同的整數(shù)值,選取不同的k的值,觀察所用時間4、 實驗分析理論上看,k的值選取為20到25較好;但是,從實際上來看,當(dāng)k為50左右時間為39毫秒,最少,但不同的時刻運行后的時間都不相同,而且不同的輸入時刻的運行時間也不相同,當(dāng)數(shù)據(jù)較大時候,對k 的值的選取有會有所不同,同時不同性能的機器對測試結(jié)果也不同,所以對于k值的選取沒有固定的數(shù)值。#include#includeusing namespace std;#define M 40void swap

3、(int * a,int * b) int tem; tem=*a; *a=*b; *b=tem;int partition(int v,const int low,const int high)int i,pivotpos,pivot;pivotpos=low;pivot=vlow;for(i=low+1;i=high;+i) if(vipivot) pivotpos+; if(pivotpos!=i)swap(vi,vpivotpos); vlow=vpivotpos;vpivotpos=pivot;/coutthe partition function is calledn;return

4、 pivotpos;/*void QuickSort(int a, const int low,const int high)int item; if(lowhigh) item=partition(a,low,high); QuickSort(a,low,item-1); QuickSort(a,item+1,high); */void QuickSort(int a, const int low,const int high)int item;if(high-low=M)return; if(lowhigh) item=partition(a,low,high); QuickSort(a,

5、low,item-1); QuickSort(a,item+1,high); / coutthe QuickSort is calledendl;void InsertSort(int a,const int low,const int high)int i,j;int tem;for(i=1;i=0&temaj) aj+1=aj; j-; aj+1=tem;/coutthe InsertSort is calledendl;void HybridSort(int a,const int low,const int high) QuickSort(a,low,high); InsertSort

6、(a,low,high); coutthe HybidSort is calledendl;int main() int i,a100;/int *a=NULL;long int t;struct timeb t1,t2;/*coutplease input the number of the element:n;a = (int*)malloc(n*sizeof(int);coutplease input every element:endl;*/for( i=0; i100; i+)ai=i+10;/QuickSort(a,0,n-1);ftime(&t1);HybridSort(a,0,99);cout after sorted quickly,the result isendl;for(i=0; i100; i+)coutai ;if(i%10=0)coutendl;coutendl;ftime(&t2);t=(t2.time-t1.time)*1000+(litm); /*

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論