



下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Merge Sort and QuicksortPrerequisites, Goals, and OutcomesPrerequisitesStude nts should have mastered the followi ng prerequisite skills.« MergeSort -Merge Sort Algorithm* QuickSort - QuickSort AlgorithmGoals: This programming assignmentsimproves the performanee of merge sort by implementing a
2、non-recursive, semi-in-plaee version of the merge-sort algorithm.Outcomes: Students successfully eompleting this assignment would master the followi ng outcomes.« Un dersta nd the Merge Sort algorithm and comparis ons of differe nt algorithms« Un dersta nd the QuickSort algorithmDescriptio
3、nThe students are supposed to refer to the lecture for more information about MergeSort and QuickSort. No more material is provided in this secti on.In-Place SortingIn-place sorting is to sort data items without using additional arrays. For instanee, quick sort performs partiti oning operati ons by
4、simply repeat ing a swapp ing operatio n on two data items in a give n array, which thus n eeds no extra arrays.On the other hand, merge sort we have studied allocates a temporary array, sorts partial data items in that array, and copies them back to the orig inal array at each recursive call. Due t
5、o this repetitive array-copy ing operati ons, merge sort is much slower tha n quick sort although their running time is upper-bounded to O(n log n).It has bee n an research in terest to develop in-place merge sort algorithms, almost all of which are however impractically complicated. Yet, we can imp
6、rove the performa nee of merge sort with addi ng the followi ng two restricti ons:1. Using a non-recursive method2. Using only one additional array (i.e., a semi-i n-place method).Merge data from the orig inal in to the additi onal array at the very bottom stage, and thereafter perform the next merg
7、e from the additi onal in to the origi nal array, in which way merge operati ons are applied to the orig inal and additi onal array in turn as you go through each repetitive stage. Don't copy back in termediate results to the origi nal array at the end of each stage for the purpose of always mer
8、gi ng data from the orig inal to the additional array.Note that we still need to allow data items to be copied between the original and this additional arrays as many times asO(log n).TaskDesign and implement a non-recursive, semi-in-place version of the merge-sort algorithm. The frameworks of the t
9、hree algorithms are given in file quicksort.cpp, mergesort.cpp and mergeImproved.cpp respectively.Needless to say, mergeImproved( ) function must not call itself or some other recursive functions. Furthermore, the algorithm should still be based on the same divide-and-conquer approach.Use the main f
10、unction to verify and evaluate the performance of your non-recursive, semi-in-place merge sort program.Compare the performance among the usual quicksort, the usual mergesort, and your improved mergesort as increasing the array size = 10, 100, 1000, and 10000.SubmitSubmit only the following. quicksor
11、t.cpp mergesort.cpp mergeImproved.cpp#include<iostream> using namespace std;#include<iostream>using namespace std; class sort private: int Array10; int left,right;/merge sorting public:sort();void MergeSort(int Array,int TempArray,int left,int right) int middle; if(left<right) middle=
12、(left+right)/2;MergeSort(Array,TempArray,left,middle);MergeSort(Array,TempArray,middle+1,right);Merge(Array,TempArray,left,right,middle);void Merge(int Array,int TempArray,int left,int right,int middle)int i,j,index1,index2;for(j=left;j<=right;j+)TempArrayj=Arrayj;index1=left;index2=middle+1;i=le
13、ft;while(index1<=middle&&index2<=right)if(TempArrayindex1<=TempArrayindex2)Arrayi+=TempArrayindex1+;elseArrayi+=TempArrayindex2+;while(index1<=middle)Arrayi+=TempArrayindex1+;while(index2<=right)Arrayi+=TempArrayindex2+;/quick sortvoid QuickSort(int Array,int left,int right)if
14、(right<=left)return;int pivot=SelectPivot(left,right);swap(Array,pivot,right);pivot=Partition(Array,left,right);QuickSort(Array,left,pivot-1);QuickSort(Array,pivot+1,right);int SelectPivot(int left,int right)return (left+right)/2;void swap(int Array,int pivot,int right)int temp;Arrayright+1=Array
15、pivot-1;int Partition(int Array,int left,int right)int l=left;int r=right;int TempRecord=Arrayr;while(l!=r)while(Arrayl<=TempRecord&&r>l) l+;if(l<r)Arrayr=Arrayl;r-; while(Arrayr>=TempRecord&&r>l) r-;if(l<r)Arrayl=Arrayr;l+;Arrayl=TempRecord;return l;void main()sort t1;int Array10;int TempArray10;int i,chose;cout<<" 請輸入你要排序的數(shù) "<<endl;for(i=0;i<10;i+) cin>>Arrayi;cout<<" 需預(yù)排序的數(shù)如下所示 "<<endl; for(i=0;i<10;i+) cout<<Arrayi<<" "2 選用cout«"請選擇你的排序方案(輸入1選用MergeSo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 門窗商超轉(zhuǎn)讓合同協(xié)議
- 阿里協(xié)議解除勞動(dòng)合同
- 食品技術(shù)投資合同協(xié)議
- 雨傘組裝代加工合同協(xié)議
- 降低質(zhì)保金合同補(bǔ)充協(xié)議
- 預(yù)簽勞動(dòng)合同協(xié)議
- 韶關(guān)魚塘買賣合同協(xié)議
- 項(xiàng)目方案策劃協(xié)議書范本
- 鎮(zhèn)政府違建拆除合同協(xié)議
- 項(xiàng)目開發(fā)合作合同協(xié)議
- 2025-2030中國高碳鋼絲行業(yè)市場發(fā)展趨勢與前景展望戰(zhàn)略研究報(bào)告
- 老年人70歲駕考三力測試題庫
- 2025年中考語文復(fù)習(xí)閱讀專題 名著勾連整合及綜合訓(xùn)練 課件
- T-CECS 10395-2024 現(xiàn)制反射隔熱復(fù)合防水卷材
- 鐵路沿線危樹清理施工方案
- 統(tǒng)編版語文一年級下冊2024-2025學(xué)年度語文園地五(課件)
- 重慶市巴蜀學(xué)校2024-2025學(xué)年九年級上學(xué)期12月月考語文試題
- 《中國名牌大學(xué)簡介》課件
- 酒店防洪防汛培訓(xùn)
- 中小學(xué)校財(cái)務(wù)制度知識(shí)培訓(xùn)
- 2024年江蘇泰州市第四人民醫(yī)院招聘高層次人才15人歷年管理單位遴選500模擬題附帶答案詳解
評論
0/150
提交評論