




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、實 驗 報 告(2015/2016學年 第2學期)課程名稱數據結構A實驗名稱各種內排序算法的實現及性能的比較實驗時間2016年6月20日指導單位計算機科學與技術系指導教師駱健學生姓名班級學號學院(系) 管理學院專 業信息管理與信息系統一、 問題陳述(1) 驗證教材的各種內排序算法(2) 分析各種內排序算法的時間復雜度(3) 改進教材中的快速排序法,使得當子集和小于10個元素時改用直接插入排序(4) 使用隨機數發生器產生大數據集合,運行上述 各排序算法,使系統時鐘測量個算法所需的實際時間,并進行比較。系統時鐘包含在頭文件“time.h”中。二、概要設計Main.cpp調用Menu.h ,為主程序
2、。Menu.h主菜單Selectsort.h簡單選擇排序Insertsort.h直接插入排序Bubblesort.h冒泡排序Quicksort.h快速排序Mergesort.h合并排序三、詳細設計簡單選擇排序:將初始序列(A0An-1)作為待排序序列,第一趟在待排序序列(A0An-1)中找最小元素,與該序列中第一個元素A0交換,這樣子序列(A0)有序,下一趟排序在帶牌子序列(A1An-1)中進行。第i趟排序子序列(Ai-1An-1)中進行。經過n-1趟排序后使得初始序列有序。程序流程圖:開始i=0i<n-1Small=ij=i+1j<nAj<AsmallSmall=jSwap
3、(Ai,Asmall)j+結束NNNYYi+直接插入排序:一個元素插入到有序表中,首先將其存入臨時變量temp,然后從后往前查找插入位置。當temp小于表中元素時,就將該元素后移一個位置,繼續比較、移動,直到temp大于等于表中元素或者到了有序表的第一個元素結束,這時就可將temp存入剛后移的元素的位置了。程序流程圖:開始i<nint j=iT temp=Aij>0&&temp<Aj-1Aj=Aj-1;j-Aj=tempi+結束NYYN冒泡排序:第一趟在序列(A0An-1)中從前往后進行兩個相鄰元素的比較,若后者小,則交換,比較n-1次;第一趟排序結束,最大元
4、素被交換到An-1中(即沉底)下一趟排序只需在子序列(A0An-2)中進行;如果在某一趟排序中未進行交換元素,說明子序列已經有序,則不再進行下一趟排序。程序流程圖:開始i=n-1i>0last=0;j=0j<iAj+1<AjSwap(Aj,Aj+1)Last=ji=last結束NNYY快速排序:取第一個元素作為分割元素,初始化i=left,j=right+1,i從左往右尋找第一個大于等于分割元素的元素,j從右往左找第一個小于等于分割元素的元素并交換,i和j繼續移動,重復上述步驟,直到當i>=j時將j與第一個元素交換。程序流程圖:開始left<righti=left
5、;j=righti+Ai<Aleftj-Aj>Alefti<jSwap(Ai,Aj)i<jSwap(Aleft,Aj)Qsort(A,left,j-1)Qsort(A,j+1,right)結束NYYNYNNYYN兩路合并排序:將有n 個元素的序列看成是n個長度為1的有序子序列,然后兩兩合并子序列,得到n/2個長度為2或1的有序子序列,再兩兩合并直到得到一個長度為n的有序序列時結束。程序流程圖:開始int size=1Size<ni1=1i1+size<ni2=i1+sizej1=i2-1i2+size-1>n-1j2=n-1Merge(A,i1,j1,
6、i2,j2)i1=j2+1size*=2結束j2=i2+size-1NYYNNY四、程序代碼selectsort.h#include <iostream.h>/簡單選擇排序template <class T>void SelectSort(T A, int n) int small;for (int i=0; i<n-1; i+) small=i;for(int j=i+1;j<n;j+)if(Aj<Asmall)small=j;Swap(Ai,Asmall);Insertsort.h#include <iostream.h>/直接插入排序
7、template <class T>void InsertSort(T A, int n) for(int i=1; i<n; i+) int j=i;T temp=Ai;while(j>0&&temp<Aj-1)Aj=Aj-1;j-;Aj=temp;/*ok!*/Bubblesort.h#include <iostream.h>template <class T>void BubbleSort(T A, int n) int i,j,last; i=n-1; while(i>0) last=0; for(j=0;j&
8、lt;i;j+) if(Aj+1<Aj) Swap(Aj,Aj+1); last=j; i=last; Quicksort.h#include <iostream.h>/改進的快速排序template<class T>void quick(T A,int n)int *a; int top=0,right,left,j; a=new intn;if(a=NULL) return;atop+=0;atop+=n-1; /lcfor(j=0;aj!=NULL;j+) left=aj+;right=aj; if(left>right)Swap(left,right
9、); if(right-left<15)InsertSortExt(A,left,right); elseatop+=left; atop+=QuickSort(A,left,right)-1;atop+=atop-2+2;atop+=right; template<class T>int QuickSort(T A,int left,int right) int i,j;if(left<right)i=left;j=right+1;dodo i+;while(Ai<Aleft);do j-;while(Aj>Aleft);if(i<j)Swap(Ai
10、,Aj);while(i<j);Swap(Aleft,Aj);return j;return 0;template<class T>void InsertSortExt(T A,int left,int right) for(int i=left+1; i<right; i+) int j=i;T temp=Ai; while (j>0 && temp<Aj-1) Aj=Aj-1; j-; Aj=temp; Mergesort.h#include <iostream.h>/兩路合并的C+程序template <class T
11、>void Merge(T A,int i1,int j1,int i2,int j2) T *Temp=new Tj2-i1+1; int i=i1,j=i2,k=0; while(i<=j1&&j<=j2)if(Ai<=Aj)Tempk+=Ai+;else Tempk+=Aj+;while(i<=j1)Tempk+=Ai+;while(j<=j2)Tempk+=Aj+;for(i=0;i<k;i+)Ai1+=Tempi;delete Temp; /合并排序的C+程序template <class T>void Merge
12、Sort(T A, int n)int i1,j1,i2,j2; int size=1; while(size<n)i1=0;while(i1+size<n)i2=i1+size;j1=i2-1;if(i2+size-1>n-1)j2=n-1;else j2=i2+size-1;Merge(A,i1,j1,i2,j2);i1=j2+1;size*=2;Meau.h#include <iostream.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include
13、"selectsort.h"#include "insertsort.h"#include "bubblesort.h"#include "quicksort.h"#include "mergesort.h"#define SIZE 400#define TIMES 1000template <class T>class Menupublic:void printmenu();void selectsort();/簡單選擇排序void insertSort();/直接插入排序void
14、 bubbleSort();/冒泡排序void quickSort();/快速排序void mergeSort();/兩路合并排序void childmenu();/子菜單1void childmenu2();/子菜單2void switcha();private:int a,b,c;template <class T>void Menu<T>:printmenu()cout<<"-內排序測試系統-"<<endl;cout<<" "<<endl<<endl<<
15、endl;cout<<"1.簡單選擇排序"<<endl;cout<<"2.直接插入排序"<<endl;cout<<"3.冒泡排序"<<endl;cout<<"4.快速排序"<<endl;cout<<"5.兩路合并排序"<<endl;cout<<"6.退出"<<endl;this->switcha();template <c
16、lass T>void Menu<T>:childmenu()cout<<"-"<<endl;cout<<"1.最好情況"<<endl;cout<<"2.最壞情況"<<endl;cout<<"3.平均情況"<<endl;cout<<"4.返回主菜單"<<endl;cin>>b;if(b=4)this->printmenu();template
17、<class T>void Menu<T>:childmenu2()cout<<"-"<<endl;cout<<"1.原始算法"<<endl;cout<<"2.改進算法"<<endl;cout<<"3.返回主菜單"<<endl;cin>>c;if(c=3)this->printmenu();template <class T>void Menu<T>:sw
18、itcha()/cout<<"ok"<<endl;cin>>a;switch(a)case 1:this->selectsort();break;/okcase 2:this->insertSort();break;/okcase 3:this->bubbleSort();break;/okcase 4:this->quickSort();break;/okcase 5:this->mergeSort();break;/okcase 6:exit(1);break;default:cout<<&q
19、uot;error"<<endl;this->printmenu();break;template <class T>void printout(T A,int n)/打印數組,測試時用for(int i=0;i<n;i+)cout<<Ai<<" "cout<<endl;template <class T>T *producedate(int x)/產生順序,逆序,隨機的數組int i;T *A=new TSIZE;switch(x)case 1:for(i=0;i<SIZE
20、;i+)Ai=i;return A;/順序break;case 2:for(i=SIZE;i>0;i-)Ai-1=SIZE-i;return A;/逆序break;case 3:srand(time(NULL);for(i=0;i<SIZE;i+)Ai=rand()%1000+1;return A;/隨機break;default:cout<<"error"<<endl;return A;break;template <class T>void Swap(T &a,T &b)/交換2個元素T temp=a;a=
21、b;b=temp;template <class T>void Menu<T>:bubbleSort()cout<<"冒泡排序"<<endl;this->childmenu();T *A;double duration;clock_t start,finish;start=clock();cout<<"ok"<<endl;for(int i=0;i<TIMES;i+)A=producedate<T>(b);BubbleSort(A,SIZE);delete A
22、;finish=clock();duration=(double)(finish-start)/CLOCKS_PER_SEC;/printout(A,SIZE);cout<<"用時: "<<duration<<endl;system("pause");/delete A;this->bubbleSort();/*ok*/template <class T>void Menu<T>:insertSort()cout<<"直接插入排序"<<endl;
23、this->childmenu();T *A;double duration;/A=producedate<T>(b);/if(A=NULL)cout<<"error"delete A;this->insertSort();/printout(A,SIZE);clock_t start,finish;start=clock();cout<<"ok"<<endl;for(int i=0;i<TIMES;i+)A=producedate<T>(b);InsertSort(A,SIZ
24、E);delete A;finish=clock();duration=(double)(finish-start)/CLOCKS_PER_SEC;/printout(A,SIZE);cout<<"用時: "<<duration<<endl;system("pause");/delete A;this->insertSort();template <class T>void Menu<T>:mergeSort()/this->childmenu();cout<<"
25、;合并排序"<<endl;cout<<"直接用隨機數據測試"<<endl;T *A;double duration;clock_t start,finish;start=clock();cout<<"ok"<<endl;for(int i=0;i<TIMES;i+)A=producedate<T>(3);MergeSort(A,SIZE);delete A;finish=clock();duration=(double)(finish-start)/CLOCKS_PE
26、R_SEC;/printout(A,SIZE);cout<<"用時: "<<duration<<endl;system("pause");/delete A;this->printmenu();/*ok*/template<class T>void Menu<T>:quickSort()this->childmenu2();T *A;double duration;clock_t start,finish;if(c=1)cout<<"原始快速排序"&l
27、t;<endl;cout<<"直接用隨機數據測試"<<endl;start=clock();cout<<"ok"<<endl;for(int i=0;i<TIMES;i+)A=producedate<T>(3);quick(A,SIZE);delete A;finish=clock();duration=(double)(finish-start)/CLOCKS_PER_SEC;cout<<"用時: "<<duration<<e
28、ndl;system("pause");this->quickSort();else if(c=2)cout<<"改進的快速排序"<<endl;cout<<"直接用隨機數據測試"<<endl;/*A=producedate<T>(3);printout(A,SIZE);quick(A,SIZE);printout(A,SIZE);delete A;this->printmenu();*/T *A;start=clock();cout<<"ok"<<endl;for(int i=0;i<TIMES;i+)A=producedate<T>(3);quick(A,SIZE);delete A;finish=clock();duration=(double)(finish-start)/CLOCKS_PER_SEC;cou
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業環境管理體系認證與財務管理考核試卷
- 化肥銷售合同中的合同履行中的質量爭議處理流程考核試卷
- 人造板回收利用技術探討考核試卷
- 材料生物降解速率測試考核試卷
- 殘障人士運動訓練跨學科合作研究考核試卷
- 貨物驗收報告編制規范考核試卷
- 三顧茅廬讀書筆記15篇
- 口腔護理發展史
- 檢察文化建設年活動方案
- 汽車全國活動方案
- GB/T 45719-2025半導體器件金屬氧化物半導體(MOS)晶體管的熱載流子試驗
- 寶媽日常心理護理
- 2025年社會學概論測試題含答案(附解析)
- 2025-2030年環境工程產業深度調研及發展趨勢與投資戰略研究報告
- 2025年事業單位公開招聘考試(E類)《綜合應用能力西醫臨床》試卷真題及完整解析
- 保險公司保單管理制度
- 2025年中國AI翻譯行業市場全景分析及前景機遇研判報告
- 2025-2030中國酶聯免疫吸附測定(ELISA)行業市場發展趨勢與前景展望戰略研究報告
- 2025年內蒙古眾達人力資源公司招聘題庫帶答案分析
- 水利工程隱患排查課件
- 醫藥公司廉政管理制度
評論
0/150
提交評論