排序算法總結(jié)

排序算法總結(jié)

分類(lèi)?編程技術(shù)

排序算法平均時(shí)間復(fù)雜度

冒泡排序O(n2)

選擇排序O(n2)

插入排序O(n2)

希爾排序O(n1.5)

快速排序O(N*logN)

歸并排序O(N*logN)

堆排序O(N*logN)

基數(shù)排序O(d(n+r))

一. 冒泡排序(BubbleSort)

基本思想:兩個(gè)數(shù)比較大小,較大的數(shù)下沉,較小的數(shù)冒起來(lái)。

過(guò)程:

比較相鄰的兩個(gè)數(shù)據(jù),如果第二個(gè)數(shù)小,就交換位置。

從后向前兩兩比較,一直到比較最前兩個(gè)數(shù)據(jù)。最終最小數(shù)被交換到起始的位置,這樣第一個(gè)最小數(shù)的位置就排好了。

繼續(xù)重復(fù)上述過(guò)程,依次將第2.3...n-1個(gè)最小數(shù)排好位置。

冒泡排序

平均時(shí)間復(fù)雜度:O(n2)

java代碼實(shí)現(xiàn):

/**

* 冒泡排序 大到小

* @param arr

*/

private static void BubbleSort(int[] arr) {

for(int i = 0;i<arr.length-1; i++) {

for(int j = arr.length-1;j>i;j--) {//大到小

if (arr[j] > arr[j-1]) {

int temp = arr[j];

arr[j] = arr[j-1];

arr[j-1] = temp;

}

}

}

}

/**

* 冒泡排序 小到大

* @param arr

*/

private static void BubbleSort2(int[] arr) {

for(int i = 0;i<arr.length-1; i++) {

for(int j = 0;j<arr.length-1-i;j++) {//小到大

if (arr[j] > arr[j+1]) {

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

優(yōu)化:

針對(duì)問(wèn)題:

數(shù)據(jù)的順序排好之后,冒泡算法仍然會(huì)繼續(xù)進(jìn)行下一輪的比較,直到arr.length-1次,后面的比較沒(méi)有意義的。

方案:

設(shè)置標(biāo)志位flag,如果發(fā)生了交換flag設(shè)置為true;如果沒(méi)有交換就設(shè)置為false。

這樣當(dāng)一輪比較結(jié)束后如果flag仍為false,即:這一輪沒(méi)有發(fā)生交換,說(shuō)明數(shù)據(jù)的順序已經(jīng)排好,沒(méi)有必要繼續(xù)進(jìn)行下去。

public static void BubbleSort2(int[] arr) {

int temp;// 臨時(shí)變量

boolean flag;// 是否交換的標(biāo)志

for (int i = 0; i < arr.length - 1; i++) { // 表示趟數(shù),一共arr.length-1次。

flag = false;

for (int j = arr.length - 1; j > i; j--) {

if (arr[j] < arr[j - 1]) {

temp = arr[j];

arr[j] = arr[j - 1];

arr[j - 1] = temp;

flag = true;

}

}

if (!flag)

break;

}

}

二. 選擇排序(SelctionSort)

基本思想:

在長(zhǎng)度為N的無(wú)序數(shù)組中,第一次遍歷n-1個(gè)數(shù),找到最小的數(shù)值與第一個(gè)元素交換;

第二次遍歷n-2個(gè)數(shù),找到最小的數(shù)值與第二個(gè)元素交換;

。。。

第n-1次遍歷,找到最小的數(shù)值與第n-1個(gè)元素交換,排序完成。

過(guò)程:

選擇排序

平均時(shí)間復(fù)雜度:O(n2)

java代碼實(shí)現(xiàn):

public static void selectSort(int array[], int lenth) {

for (int i = 0; i < lenth - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < lenth; j++) {

if (array[j] < array[minIndex]) {

minIndex = j;

}

}

if (minIndex != i) {

int temp = array[i];

array[i] = array[minIndex];

array[minIndex] = temp;

}

}

}

三. 插入排序(Insertion Sort)

基本思想:

在要排序的一組數(shù)中,假定前n-1個(gè)數(shù)已經(jīng)排好序,現(xiàn)在將第n個(gè)數(shù)插到前面的有序數(shù)列中,使得這n個(gè)數(shù)也是排好順序的。如此反復(fù)循環(huán),直到全部排好順序。

過(guò)程:

插入排序

相同的場(chǎng)景

平均時(shí)間復(fù)雜度:O(n2)

java代碼實(shí)現(xiàn):

/**

* 插入排序 小到大

*

* @param arr

*/

private static void insertSort(int[] arr) {

int index;

for (int i = 0; i < arr.length; i++) {

index = arr[i];

while (i > 0 && index < arr[i - 1]) {

arr[i] = arr[i - 1];

i--;

}

arr[i] = index;

}

}

四. 希爾排序(Shell Sort)

前言:

數(shù)據(jù)序列1: 13-17-20-42-28 利用插入排序,13-17-20-28-42. Number of swap:1;

數(shù)據(jù)序列2: 13-17-20-42-14 利用插入排序,13-14-17-20-42. Number of swap:3;

如果數(shù)據(jù)序列基本有序,使用插入排序會(huì)更加高效。

基本思想:

在要排序的一組數(shù)中,根據(jù)某一增量分為若干子序列,并對(duì)子序列分別進(jìn)行插入排序。

然后逐漸將增量減小,并重復(fù)上述過(guò)程。直至增量為1,此時(shí)數(shù)據(jù)序列基本有序,最后進(jìn)行插入排序。

過(guò)程:

希爾排序

平均時(shí)間復(fù)雜度:

java代碼實(shí)現(xiàn):

public static void shell_sort(int array[],int lenth){? int temp = 0;? int incre = lenth;? while(true){? ? ? incre = incre/2;? ? ? for(int k = 0;k<incre;k++){? ? //根據(jù)增量分為若干子序列? ? ? ? ? for(int i=k+incre;i<lenth;i+=incre){? ? ? ? ? ? ? for(int j=i;j>k;j-=incre){? ? ? ? ? ? ? ? ? if(array[j]<array[j-incre]){? ? ? ? ? ? ? ? ? ? ? temp = array[j-incre];? ? ? ? ? ? ? ? ? ? ? array[j-incre] = array[j];? ? ? ? ? ? ? ? ? ? ? array[j] = temp;? ? ? ? ? ? ? ? ? }else{? ? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? }? ? ? ? ? }? ? ? }? ? ? if(incre == 1){? ? ? ? ? break;? ? ? }? }}

五. 快速排序(Quicksort)

基本思想:(分治)

先從數(shù)列中取出一個(gè)數(shù)作為key值;

將比這個(gè)數(shù)小的數(shù)全部放在它的左邊,大于或等于它的數(shù)全部放在它的右邊;

對(duì)左右兩個(gè)小數(shù)列重復(fù)第二步,直至各區(qū)間只有1個(gè)數(shù)。

輔助理解:挖坑填數(shù)

初始時(shí) i = 0; j = 9; key=72

由于已經(jīng)將a[0]中的數(shù)保存到key中,可以理解成在數(shù)組a[0]上挖了個(gè)坑,可以將其它數(shù)據(jù)填充到這來(lái)。

從j開(kāi)始向前找一個(gè)比key小的數(shù)。當(dāng)j=8,符合條件,a[0] = a[8] ; i++ ;?將a[8]挖出再填到上一個(gè)坑a[0]中。

這樣一個(gè)坑a[0]就被搞定了,但又形成了一個(gè)新坑a[8],這怎么辦了?簡(jiǎn)單,再找數(shù)字來(lái)填a[8]這個(gè)坑。

這次從i開(kāi)始向后找一個(gè)大于key的數(shù),當(dāng)i=3,符合條件,a[8] = a[3] ; j-- ;?將a[3]挖出再填到上一個(gè)坑中。

數(shù)組:72 - 6 - 57 - 88 - 60 - 42 - 83 - 73 - 48 - 85 0? 1? 2? ? 3? ? 4? ? 5? ? 6? ? 7? ? 8? ? 9

此時(shí) i = 3; j = 7; key=72

再重復(fù)上面的步驟,先從后向前找,再?gòu)那跋蚝笳摇?/p>

從j開(kāi)始向前找,當(dāng)j=5,符合條件,將a[5]挖出填到上一個(gè)坑中,a[3] = a[5]; i++;

從i開(kāi)始向后找,當(dāng)i=5時(shí),由于i==j退出。

此時(shí),i = j = 5,而a[5]剛好又是上次挖的坑,因此將key填入a[5]。

數(shù)組:48 - 6 - 57 - 88 - 60 - 42 - 83 - 73 - 88 - 85 0? 1? 2? ? 3? ? 4? ? 5? ? 6? ? 7? ? 8? ? 9

可以看出a[5]前面的數(shù)字都小于它,a[5]后面的數(shù)字都大于它。因此再對(duì)a[0…4]和a[6…9]這二個(gè)子區(qū)間重復(fù)上述步驟就可以了。

<數(shù)組:48 - 6 - 57 - 42 - 60 - 72 - 83 - 73 - 88 - 85

0? 1? 2? ? 3? ? 4? ? 5? ? 6? ? 7? ? 8? ? 9

平均時(shí)間復(fù)雜度:O(N*logN)

代碼實(shí)現(xiàn):

public static void quickSort(int a[],int l,int r){? ? if(l>=r)? ? ? return;? ? int i = l; int j = r; int key = a[l];//選擇第一個(gè)數(shù)為key? ? while(i<j){? ? ? ? while(i<j && a[j]>=key)//從右向左找第一個(gè)小于key的值? ? ? ? ? ? j--;? ? ? ? if(i<j){? ? ? ? ? ? a[i] = a[j];? ? ? ? ? ? i++;? ? ? ? }? ? ? ? while(i<j && a[i]<key)//從左向右找第一個(gè)大于key的值? ? ? ? ? ? i++;? ? ? ? if(i<j){? ? ? ? ? ? a[j] = a[i];? ? ? ? ? ? j--;? ? ? ? }? ? }? ? //i == j? ? a[i] = key;? ? quickSort(a, l, i-1);//遞歸調(diào)用? ? quickSort(a, i+1, r);//遞歸調(diào)用 }

key值的選取可以有多種形式,例如中間數(shù)或者隨機(jī)數(shù),分別會(huì)對(duì)算法的復(fù)雜度產(chǎn)生不同的影響。

六. 歸并排序(Merge Sort)

基本思想:參考

歸并排序是建立在歸并操作上的一種有效的排序算法。該算法是采用分治法的一個(gè)非常典型的應(yīng)用。

首先考慮下如何將2個(gè)有序數(shù)列合并。這個(gè)非常簡(jiǎn)單,只要從比較2個(gè)數(shù)列的第一個(gè)數(shù),誰(shuí)小就先取誰(shuí),取了后就在對(duì)應(yīng)數(shù)列中刪除這個(gè)數(shù)。然后再進(jìn)行比較,如果有數(shù)列為空,那直接將另一個(gè)數(shù)列的數(shù)據(jù)依次取出即可。

//將有序數(shù)組a[]和b[]合并到c[]中void MemeryArray(int a[], int n, int b[], int m, int c[]){ int i, j, k; i = j = k = 0; while (i < n && j < m) {? ? if (a[i] < b[j])? ? ? ? c[k++] = a[i++];? ? else? ? ? ? c[k++] = b[j++];

} while (i < n)? ? c[k++] = a[i++]; while (j < m)? ? c[k++] = b[j++];}

解決了上面的合并有序數(shù)列問(wèn)題,再來(lái)看歸并排序,其的基本思路就是將數(shù)組分成2組A,B,如果這2組組內(nèi)的數(shù)據(jù)都是有序的,那么就可以很方便的將這2組數(shù)據(jù)進(jìn)行排序。如何讓這2組組內(nèi)數(shù)據(jù)有序了?

可以將A,B組各自再分成2組。依次類(lèi)推,當(dāng)分出來(lái)的小組只有1個(gè)數(shù)據(jù)時(shí),可以認(rèn)為這個(gè)小組組內(nèi)已經(jīng)達(dá)到了有序,然后再合并相鄰的2個(gè)小組就可以了。這樣通過(guò)先遞歸的分解數(shù)列,再合并數(shù)列就完成了歸并排序。

過(guò)程:

歸并排序

平均時(shí)間復(fù)雜度:O(NlogN)

歸并排序的效率是比較高的,設(shè)數(shù)列長(zhǎng)為N,將數(shù)列分開(kāi)成小數(shù)列一共要logN步,每步都是一個(gè)合并有序數(shù)列的過(guò)程,時(shí)間復(fù)雜度可以記為O(N),故一共為O(N*logN)。

代碼實(shí)現(xiàn):

public static void merge_sort(int a[],int first,int last,int temp[]){? if(first < last){? ? ? int middle = (first + last)/2;? ? ? merge_sort(a,first,middle,temp);//左半部分排好序? ? ? merge_sort(a,middle+1,last,temp);//右半部分排好序? ? ? mergeArray(a,first,middle,last,temp); //合并左右部分? }}

//合并 :將兩個(gè)序列a[first-middle],a[middle+1-end]合并public static void mergeArray(int a[],int first,int middle,int end,int temp[]){? ?

? int i = first;? int m = middle;? int j = middle+1;? int n = end;? int k = 0;

? while(i<=m && j<=n){? ? ? if(a[i] <= a[j]){? ? ? ? ? temp[k] = a[i];? ? ? ? ? k++;? ? ? ? ? i++;? ? ? }else{? ? ? ? ? temp[k] = a[j];? ? ? ? ? k++;? ? ? ? ? j++;? ? ? }? }? ?

? while(i<=m){? ? ? temp[k] = a[i];? ? ? k++;? ? ? i++;? }? ?

? while(j<=n){? ? ? temp[k] = a[j];? ? ? k++;? ? ? j++;

? }? for(int ii=0;ii<k;ii++){? ? ? a[first + ii] = temp[ii];? }}

七. 堆排序(HeapSort)

基本思想:

圖示:?(88,85,83,73,72,60,57,48,42,6)

Heap Sort

平均時(shí)間復(fù)雜度:O(NlogN)

由于每次重新恢復(fù)堆的時(shí)間復(fù)雜度為O(logN),共N - 1次重新恢復(fù)堆操作,再加上前面建立堆時(shí)N / 2次向下調(diào)整,每次調(diào)整時(shí)間復(fù)雜度也為O(logN)。二次操作時(shí)間相加還是O(N * logN)。

java代碼實(shí)現(xiàn):

//構(gòu)建最小堆public static void MakeMinHeap(int a[], int n){ for(int i=(n-1)/2 ; i>=0 ; i--){? ? MinHeapFixdown(a,i,n); }}//從i節(jié)點(diǎn)開(kāi)始調(diào)整,n為節(jié)點(diǎn)總數(shù) 從0開(kāi)始計(jì)算 i節(jié)點(diǎn)的子節(jié)點(diǎn)為 2*i+1, 2*i+2? public static void MinHeapFixdown(int a[],int i,int n){? int j = 2*i+1; //子節(jié)點(diǎn)? int temp = 0;? while(j<n){? ? ? //在左右子節(jié)點(diǎn)中尋找最小的? ? ? if(j+1<n && a[j+1]<a[j]){?

? ? ? ? ? j++;? ? ? }? ? ? if(a[i] <= a[j])? ? ? ? ? break;? ? ? //較大節(jié)點(diǎn)下移? ? ? temp = a[i];? ? ? a[i] = a[j];? ? ? a[j] = temp;? ? ? i = j;? ? ? j = 2*i+1;? }}

public static void MinHeap_Sort(int a[],int n){? int temp = 0;? MakeMinHeap(a,n);? for(int i=n-1;i>0;i--){? ? ? temp = a[0];? ? ? a[0] = a[i];? ? ? a[i] = temp;

? ? ? MinHeapFixdown(a,0,i);? }? ? }

八. 基數(shù)排序(RadixSort)

BinSort

基本思想:

BinSort想法非常簡(jiǎn)單,首先創(chuàng)建數(shù)組A[MaxValue];然后將每個(gè)數(shù)放到相應(yīng)的位置上(例如17放在下標(biāo)17的數(shù)組位置);最后遍歷數(shù)組,即為排序后的結(jié)果。

圖示:

BinSort

問(wèn)題:?當(dāng)序列中存在較大值時(shí),BinSort 的排序方法會(huì)浪費(fèi)大量的空間開(kāi)銷(xiāo)。

RadixSort

基本思想:?基數(shù)排序是在BinSort的基礎(chǔ)上,通過(guò)基數(shù)的限制來(lái)減少空間的開(kāi)銷(xiāo)。

過(guò)程:

過(guò)程1

過(guò)程2

(1)首先確定基數(shù)為10,數(shù)組的長(zhǎng)度也就是10.每個(gè)數(shù)34都會(huì)在這10個(gè)數(shù)中尋找自己的位置。

(2)不同于BinSort會(huì)直接將數(shù)34放在數(shù)組的下標(biāo)34處,基數(shù)排序是將34分開(kāi)為3和4,第一輪排序根據(jù)最末位放在數(shù)組的下標(biāo)4處,第二輪排序根據(jù)倒數(shù)第二位放在數(shù)組的下標(biāo)3處,然后遍歷數(shù)組即可。

java代碼實(shí)現(xiàn):

public static void RadixSort(int A[],int temp[],int n,int k,int r,int cnt[]){? //A:原數(shù)組? //temp:臨時(shí)數(shù)組? //n:序列的數(shù)字個(gè)數(shù)? //k:最大的位數(shù)2? //r:基數(shù)10? //cnt:存儲(chǔ)bin[i]的個(gè)數(shù)? for(int i=0 , rtok=1; i<k ; i++ ,rtok = rtok*r){? ? ? //初始化? ? ? for(int j=0;j<r;j++){? ? ? ? ? cnt[j] = 0;? ? ? }? ? ? //計(jì)算每個(gè)箱子的數(shù)字個(gè)數(shù)? ? ? for(int j=0;j<n;j++){? ? ? ? ? cnt[(A[j]/rtok)%r]++;? ? ? }? ? ? //cnt[j]的個(gè)數(shù)修改為前j個(gè)箱子一共有幾個(gè)數(shù)字? ? ? for(int j=1;j<r;j++){? ? ? ? ? cnt[j] = cnt[j-1] + cnt[j];? ? ? }? ? ? for(int j = n-1;j>=0;j--){? ? ? //重點(diǎn)理解? ? ? ? ? cnt[(A[j]/rtok)%r]--;? ? ? ? ? temp[cnt[(A[j]/rtok)%r]] = A[j];? ? ? }? ? ? for(int j=0;j<n;j++){? ? ? ? ? A[j] = temp[j];? ? ? }? }}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容