![數(shù)據(jù)結(jié)構(gòu)各種排序算法總結(jié)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f1.gif)
![數(shù)據(jù)結(jié)構(gòu)各種排序算法總結(jié)_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f2.gif)
![數(shù)據(jù)結(jié)構(gòu)各種排序算法總結(jié)_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f3.gif)
![數(shù)據(jù)結(jié)構(gòu)各種排序算法總結(jié)_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f4.gif)
![數(shù)據(jù)結(jié)構(gòu)各種排序算法總結(jié)_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-2/23/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f/997cbef8-48ca-42a0-b4f3-c05a8df0ed0f5.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、 數(shù)據(jù)結(jié)構(gòu)各種排序算法總結(jié)計算機排序與人進行排序的不同:計算機程序不能象人一樣通覽所有的數(shù)據(jù),只能根據(jù)計算機的"比較"原理,在同一時間內(nèi)對兩個隊員進行比較,這是算法的一種"短視"。1. 冒泡排序 BubbleSort最簡單的一個public void bubbleSort() int out, in; for(out=nElems-1; out>0; out-) / outer loop (backward) for(in=0; in<out; in+) / inner loop (forward) if( ain > ain+1 )
2、/ out of order? swap(in, in+1); / swap them / end bubbleSort()效率:O(N2)2. 選擇排序 selectSortpublic void selectionSort() int out, in, min; for(out=0; out<nElems-1; out+) / outer loop min = out; / minimum for(in=out+1; in<nElems; in+) / inner loop if(ain < amin ) / if min greater, min = in; / we
3、have a new min swap(out, min); / swap them / end for(out) / end selectionSort()效率:O(N2)3. 插入排序 insertSort在插入排序中,一組數(shù)據(jù)在某個時刻實局部有序的,為在冒泡和選擇排序中實完全有序的。public void insertionSort() int in, out; for(out=1; out<nElems; out+) / out is dividing line long temp = aout; / remove marked item in = out; / start sh
4、ifts at out while(in>0 && ain-1 >= temp) / until one is smaller, ain = ain-1; / shift item to right -in; / go left one position ain = temp; / insert marked item / end for / end insertionSort()效率:比冒泡排序快一倍,比選擇排序略快,但也是O(N2)如果數(shù)據(jù)基本有序,幾乎需要O(N)的時間4. 歸并排序 mergeSort利用遞歸,不斷的分割數(shù)組,然后歸并有序數(shù)組效率為O(N*l
5、ogN),缺點是需要在存儲器中有一個大小等于被排序的數(shù)據(jù)項數(shù)目的數(shù)組。public void mergeSort() / called by main() / provides workspace long workSpace = new longnElems; recMergeSort(workSpace, 0, nElems-1); /- private void recMergeSort(long workSpace, int lowerBound, int upperBound) if(lowerBound = upperBound) / if range is 1, return;
6、/ no use sorting else / find midpoint int mid = (lowerBound+upperBound) / 2; / sort low half recMergeSort(workSpace, lowerBound, mid); / sort high half recMergeSort(workSpace, mid+1, upperBound); / merge them merge(workSpace, lowerBound, mid+1, upperBound); / end else / end recMergeSort() /- private
7、 void merge(long workSpace, int lowPtr, int highPtr, int upperBound) int j = 0; / workspace index int lowerBound = lowPtr; int mid = highPtr-1; int n = upperBound-lowerBound+1; / # of items while(lowPtr <= mid && highPtr <= upperBound) if( theArraylowPtr < theArrayhighPtr ) workSpac
8、ej+ = theArraylowPtr+; else workSpacej+ = theArrayhighPtr+; while(lowPtr <= mid) workSpacej+ = theArraylowPtr+; while(highPtr <= upperBound) workSpacej+ = theArrayhighPtr+; for(j=0; j<n; j+) theArraylowerBound+j = workSpacej; / end merge()5. 希爾排序 ShellSortpublic void shellSort() int inner,
9、outer; long temp; int h = 1; / find initial value of h while(h <= nElems/3) h = h*3 + 1; / (1, 4, 13, 40, 121, .) while(h>0) / decreasing h, until h=1 / h-sort the file for(outer=h; outer<nElems; outer+) temp = theArrayouter; inner = outer; / one subpass (eg 0, 4, 8) while(inner > h-1 &a
10、mp;& theArrayinner-h >= temp) theArrayinner = theArrayinner-h; inner -= h; theArrayinner = temp; / end for h = (h-1) / 3; / decrease h / end while(h>0) / end shellSort()希爾排序是基于插入排序的,由于插入排序復制的次數(shù)太多,導致效率的下降,而ShellSort先利用n-增量排序?qū)?shù)據(jù)變?yōu)榛居行?,然后在利用插入排序?-增量排序)。 n在排序中的一系列取值方法:Lnuth序列,間隔h=3h + 1效率:O(N
11、3/2) 到O(N7/6)6. 快速排序其根本機制在于劃分:劃分數(shù)據(jù)就是把數(shù)據(jù)分為兩組,使所有關(guān)鍵字大于特定值的數(shù)據(jù)項在一組,使所有關(guān)鍵字小于特定值的數(shù)據(jù)項在另一組。public int partitionIt(int left, int right, long pivot) int leftPtr = left - 1; / right of first elem int rightPtr = right + 1; / left of pivot while(true) while(leftPtr < right && / find bigger item theArr
12、ay+leftPtr < pivot) ; / (nop) while(rightPtr > left && / find smaller item theArray-rightPtr > pivot) ; / (nop) if(leftPtr >= rightPtr) / if pointers cross, break; / partition done else / not crossed, so swap(leftPtr, rightPtr); / swap elements / end while(true) return leftPtr; /
13、 return partition / end partitionIt()快速排序算法本質(zhì)上通過把一個數(shù)組劃分為兩個子數(shù)組,然后遞歸的調(diào)用自身為每一個子數(shù)組進行快速排序。樞紐(Pivot)的選擇:選擇數(shù)組最右端的數(shù)據(jù)項作為樞紐:public void recQuickSort(int left, int right) if(right-left <= 0) / if size <= 1, return; / already sorted else / size is 2 or larger long pivot = theArrayright; / rightmost item /
14、 partition range int partition = partitionIt(left, right, pivot); recQuickSort(left, partition-1); / sort left side recQuickSort(partition+1, right); / sort right side / end recQuickSort()/- public int partitionIt(int left, int right, long pivot) int leftPtr = left-1; / left (after +) int rightPtr =
15、 right; / right-1 (after -) while(true) / find bigger item while( theArray+leftPtr < pivot ) ; / (nop) / find smaller item while(rightPtr > 0 && theArray-rightPtr > pivot) ; / (nop) if(leftPtr >= rightPtr) / if pointers cross, break; / partition done else / not crossed, so swap(l
16、eftPtr, rightPtr); / swap elements / end while(true) swap(leftPtr, right); / restore pivot return leftPtr; / return pivot location / end partitionIt()當數(shù)據(jù)是有序的或者是逆序時,從數(shù)組的一端或者另外一端選擇數(shù)據(jù)項作為樞紐都不是好辦法,比如逆序時,樞紐是最小的數(shù)據(jù)項,每一次劃分都產(chǎn)生一個有N-1個數(shù)據(jù)項的子數(shù)組以及另外一個只包含樞紐的子數(shù)組三數(shù)據(jù)項取中劃分:選擇第一個、最后一個以及中間位置數(shù)據(jù)項的中值作為樞紐public void recQuick
17、Sort(int left, int right) int size = right-left+1; if(size <= 3) / manual sort if small manualSort(left, right); else / quicksort if large long median = medianOf3(left, right); int partition = partitionIt(left, right, median); recQuickSort(left, partition-1); recQuickSort(partition+1, right); / e
18、nd recQuickSort()/- public long medianOf3(int left, int right) int center = (left+right)/2; / order left & center if( theArrayleft > theArraycenter ) swap(left, center); / order left & right if( theArrayleft > theArrayright ) swap(left, right); / order center & right if( theArraycenter > theArrayright ) swap(center, right); swap(center, right-1); / put
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年武夷巖茶項目投資價值分析報告
- 2025至2030年中國真空鍍水溶色粉數(shù)據(jù)監(jiān)測研究報告
- 2025至2030年女式編織腰帶項目投資價值分析報告
- 二手房出租合同書
- 不定時工作制協(xié)議書范本
- 四方股東合伙經(jīng)營協(xié)議書范本
- 飲水機購銷合同范本
- 零星工程框架協(xié)議書范本
- 商城合作協(xié)議書范本
- 出借咨詢與服務合同
- 人教版一年級數(shù)學2024版上冊期末測評(提優(yōu)卷一)(含答案)
- 2024年同等學力申碩英語考試真題
- 浙江省杭州市2024年中考語文試卷(含答案)
- 種植二期手種植義齒II期手術(shù)護理配合流程
- 安全隱患舉報獎勵制度
- 牛津書蟲系列1-6級 雙語 4B-03.金銀島中英對照
- 2024-2025學年深圳市南山區(qū)六年級數(shù)學第一學期期末學業(yè)水平測試試題含解析
- 2024-2030年中國免疫細胞存儲行業(yè)市場發(fā)展分析及競爭形勢與投資戰(zhàn)略研究報告
- 工貿(mào)行業(yè)企業(yè)安全生產(chǎn)標準化建設(shè)實施指南
- T-CACM 1560.6-2023 中醫(yī)養(yǎng)生保健服務(非醫(yī)療)技術(shù)操作規(guī)范穴位貼敷
- 07J912-1變配電所建筑構(gòu)造
評論
0/150
提交評論