天天看點

常用排序算法/程式員必須掌握的8大排序算法

分類:

1)插入排序(直接插入排序、希爾排序)

2)交換排序(冒泡排序、快速排序)

3)選擇排序(直接選擇排序、堆排序)

4)歸并排序

5)配置設定排序(基數排序)

所需輔助空間最多:歸并排序

所需輔助空間最少:堆排序

平均速度最快:快速排序

不穩定:快速排序,希爾排序,堆排序。

先來看看8種排序之間的關系:

常用排序算法/程式員必須掌握的8大排序算法

 1.直接插入排序

(1)基本思想:在要排序的一組數中,假設前面(n-1)[n>=2] 個數已經是排

好順序的,現在要把第n個數插到前面的有序數中,使得這n個數

也是排好順序的。如此反複循環,直到全部排好順序。

(2)執行個體

常用排序算法/程式員必須掌握的8大排序算法

(3)用java實作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

package

com.njue; 

publicclass insertSort { 

public

insertSort(){ 

inta[]={

49

,

38

,

65

,

97

,

76

,

13

,

27

,

49

,

78

,

34

,

12

,

64

,

5

,

4

,

62

,

99

,

98

,

54

,

56

,

17

,

18

,

23

,

34

,

15

,

35

,

25

,

53

,

51

}; 

int

temp=

for

(

int

i=

1

;i<a.length;i++){ 

int

j=i-

1

temp=a[i]; 

for

(;j>=

&&temp<a[j];j--){ 

a[j+

1

]=a[j]; 

//将大于temp的值整體後移一個機關 

a[j+

1

]=temp; 

for

(

int

i=

;i<a.length;i++){ 

System.out.println(a[i]); 

}

2.   希爾排序(最小增量排序)

(1)基本思想:算法先将要排序的一組數按某個增量d(n/2,n為要排序數的個數)分成若幹組,每組中記錄的下标相差d.對每組中全部元素進行直接插入排序,然後再用一個較小的增量(d/2)對它進行分組,在每組中再進行直接插入排序。當增量減到1時,進行直接插入排序後,排序完成。

(2)執行個體:

常用排序算法/程式員必須掌握的8大排序算法

(3)用java實作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

publicclass shellSort { 

publicshellSort(){ 

int

a[]={

1

,

54

,

6

,

3

,

78

,

34

,

12

,

45

,

56

,

100

}; 

double

d1=a.length; 

int

temp=

while

(

true

){ 

d1= Math.ceil(d1/

2

); 

int

d=(

int

) d1; 

for

(

int

x=

;x<d;x++){ 

for

(

int

i=x+d;i<a.length;i+=d){ 

int

j=i-d; 

temp=a[i]; 

for

(;j>=

&&temp<a[j];j-=d){ 

a[j+d]=a[j]; 

a[j+d]=temp; 

if

(d==

1

){ 

break

for

(

int

i=

;i<a.length;i++){ 

System.out.println(a[i]); 

}

3.簡單選擇排序

(1)基本思想:在要排序的一組數中,選出最小的一個數與第一個位置的數交換;

然後在剩下的數當中再找最小的與第二個位置的數交換,如此循環到倒數第二個數和最後一個數比較為止。

(2)執行個體:

常用排序算法/程式員必須掌握的8大排序算法

(3)用java實作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

public

class

selectSort { 

public

selectSort(){ 

int

a[]={

1

,

54

,

6

,

3

,

78

,

34

,

12

,

45

}; 

int

position=

for

(

int

i=

;i<a.length;i++){      

int

j=i+

1

position=i; 

int

temp=a[i]; 

for

(;j<a.length;j++){ 

if

(a[j]<temp){ 

temp=a[j]; 

position=j; 

a[position]=a[i]; 

a[i]=temp; 

for

(

int

i=

;i<a.length;i++) 

System.out.println(a[i]); 

}

4.堆排序

(1)基本思想:堆排序是一種樹形選擇排序,是對直接選擇排序的有效改進。

堆的定義如下:具有n個元素的序列(h1,h2,…,hn),當且僅當滿足(hi>=h2i,hi>=2i+1)或(hi<=h2i,hi<=2i+1)(i=1,2,…,n/2)時稱之為堆。在這裡隻讨論滿足前者條件的堆。由堆的定義可以看出,堆頂元素(即第一個元素)必為最大項(大頂堆)。完全二叉樹可以很直覺地表示堆的結構。堆頂為根,其它為左子樹、右子樹。初始時把要排序的數的序列看作是一棵順序存儲的二叉樹,調整它們的存儲序,使之成為一個堆,這時堆的根節點的數最大。然後将根節點與堆的最後一個節點交換。然後對前面(n-1)個數重新調整使之成為堆。依此類推,直到隻有兩個節點的堆,并對它們作交換,最後得到有n個節點的有序序列。從算法描述來看,堆排序需要兩個過程,一是建立堆,二是堆頂與堆的最後一個元素交換位置。是以堆排序有兩個函數組成。一是建堆的滲透函數,二是反複調用滲透函數實作排序的函數。

(2)執行個體:

初始序列:46,79,56,38,40,84

建堆:

常用排序算法/程式員必須掌握的8大排序算法

交換,從堆中踢出最大數

常用排序算法/程式員必須掌握的8大排序算法

剩餘結點再建堆,再交換踢出最大數

常用排序算法/程式員必須掌握的8大排序算法

依次類推:最後堆中剩餘的最後兩個結點交換,踢出一個,排序完成。

(3)用java實作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

import

java.util.Arrays; 

publicclass HeapSort { 

inta[]={

49

,

38

,

65

,

97

,

76

,

13

,

27

,

49

,

78

,

34

,

12

,

64

,

5

,

4

,

62

,

99

,

98

,

54

,

56

,

17

,

18

,

23

,

34

,

15

,

35

,

25

,

53

,

51

}; 

public

HeapSort(){ 

heapSort(a); 

public

void

heapSort(

int

[] a){ 

System.out.println(

"開始排序"

); 

int

arrayLength=a.length; 

//循環建堆 

for

(

int

i=

;i<arrayLength-

1

;i++){ 

//建堆 

buildMaxHeap(a,arrayLength-

1

-i); 

//交換堆頂和最後一個元素 

swap(a,

,arrayLength-

1

-i); 

System.out.println(Arrays.toString(a)); 

private

void

swap(

int

[] data,

int

i,

int

j) { 

// TODO Auto-generated method stub 

int

tmp=data[i]; 

data[i]=data[j]; 

data[j]=tmp; 

//對data數組從0到lastIndex建大頂堆 

privatevoid buildMaxHeap(

int

[] data,

int

lastIndex) { 

// TODO Auto-generated method stub 

//從lastIndex處節點(最後一個節點)的父節點開始 

for

(

int

i=(lastIndex-

1

)/

2

;i>=

;i--){ 

//k儲存正在判斷的節點 

int

k=i; 

//如果目前k節點的子節點存在 

while

(k*

2

+

1

<=lastIndex){ 

//k節點的左子節點的索引 

int

biggerIndex=

2

*k+

1

//如果biggerIndex小于lastIndex,即biggerIndex+1代表的k節點的右子節點存在 

if

(biggerIndex<lastIndex){ 

//若果右子節點的值較大 

if

(data[biggerIndex]<data[biggerIndex+

1

]){ 

//biggerIndex總是記錄較大子節點的索引 

biggerIndex++; 

//如果k節點的值小于其較大的子節點的值 

if

(data[k]<data[biggerIndex]){ 

//交換他們 

swap(data,k,biggerIndex); 

//将biggerIndex賦予k,開始while循環的下一次循環,重新保證k節點的值大于其左右子節點的值 

k=biggerIndex; 

}

else

break

}

5.冒泡排序

(1)基本思想:在要排序的一組數中,對目前還未排好序的範圍内的全部數,自上而下對相鄰的兩個數依次進行比較和調整,讓較大的數往下沉,較小的往上冒。即:每當兩相鄰的數比較後發現它們的排序與排序要求相反時,就将它們互換。

(2)執行個體:

常用排序算法/程式員必須掌握的8大排序算法

(3)用java實作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

public

class

bubbleSort { 

publicbubbleSort(){ 

inta[]={

49

,

38

,

65

,

97

,

76

,

13

,

27

,

49

,

78

,

34

,

12

,

64

,

5

,

4

,

62

,

99

,

98

,

54

,

56

,

17

,

18

,

23

,

34

,

15

,

35

,

25

,

53

,

51

}; 

int

temp=

for

(

int

i=

;i<a.length-

1

;i++){ 

for

(

int

j=

;j<a.length-

1

-i;j++){ 

if

(a[j]>a[j+

1

]){ 

temp=a[j]; 

a[j]=a[j+

1

]; 

a[j+

1

]=temp; 

for

(

int

i=

;i<a.length;i++){ 

System.out.println(a[i]);   

}

6.快速排序

(1)基本思想:選擇一個基準元素,通常選擇第一個元素或者最後一個元素,通過一趟掃描,将待排序列分成兩部分,一部分比基準元素小,一部分大于等于基準元素,此時基準元素在其排好序後的正确位置,然後再用同樣的方法遞歸地排序劃分的兩部分。

(2)執行個體:

常用排序算法/程式員必須掌握的8大排序算法

(3)用java實作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

publicclass quickSort { 

inta[]={

49

,

38

,

65

,

97

,

76

,

13

,

27

,

49

,

78

,

34

,

12

,

64

,

5

,

4

,

62

,

99

,

98

,

54

,

56

,

17

,

18

,

23

,

34

,

15

,

35

,

25

,

53

,

51

}; 

publicquickSort(){ 

quick(a); 

for

(

int

i=

;i<a.length;i++){ 

System.out.println(a[i]); 

publicint getMiddle(

int

[] list,

int

low,

int

high) {   

int

tmp =list[low];   

//數組的第一個作為中軸   

while

(low < high){   

while

(low < high&& list[high] >= tmp) {   

high--;   

}   

list[low] =list[high];  

//比中軸小的記錄移到低端   

while

(low < high&& list[low] <= tmp) {   

low++;   

}   

list[high] =list[low];  

//比中軸大的記錄移到高端   

}   

list[low] = tmp;             

//中軸記錄到尾   

return

low;                  

//傳回中軸的位置   

}  

publicvoid _quickSort(

int

[] list,

int

low,

int

high) {   

if

(low < high){   

int

middle =getMiddle(list, low, high); 

//将list數組進行一分為二   

_quickSort(list, low, middle -

1

);      

//對低字表進行遞歸排序   

_quickSort(list,middle +

1

, high);      

//對高字表進行遞歸排序   

}   

publicvoid quick(

int

[] a2) {   

if

(a2.length >

) {   

//檢視數組是否為空   

_quickSort(a2,

, a2.length -

1

);   

}   

}

7、歸并排序

(1)基本排序:歸并(Merge)排序法是将兩個(或兩個以上)有序表合并成一個新的有序表,即把待排序序列分為若幹個子序列,每個子序列是有序的。然後再把有序子序列合并為整體有序序列。

(2)執行個體:

常用排序算法/程式員必須掌握的8大排序算法

(3)用java實作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

import

java.util.Arrays; 

publicclass mergingSort { 

inta[]={

49

,

38

,

65

,

97

,

76

,

13

,

27

,

49

,

78

,

34

,

12

,

64

,

5

,

4

,

62

,

99

,

98

,

54

,

56

,

17

,

18

,

23

,

34

,

15

,

35

,

25

,

53

,

51

}; 

publicmergingSort(){ 

sort(a,

,a.length-

1

); 

for

(

int

i=

;i<a.length;i++) 

System.out.println(a[i]); 

publicvoid sort(

int

[] data,

int

left,

int

right) { 

// TODO Auto-generatedmethod stub 

if

(left<right){ 

//找出中間索引 

int

center=(left+right)/

2

//對左邊數組進行遞歸 

sort(data,left,center); 

//對右邊數組進行遞歸 

sort(data,center+

1

,right); 

//合并 

merge(data,left,center,right);        

publicvoid merge(

int

[] data,

int

left,

int

center,

int

right) { 

// TODO Auto-generatedmethod stub 

int

[] tmpArr=newint[data.length]; 

int

mid=center+

1

//third記錄中間數組的索引 

int

third=left; 

int

tmp=left; 

while

(left<=center&&mid<=right){ 

//從兩個數組中取出最小的放入中間數組 

if

(data[left]<=data[mid]){ 

tmpArr[third++]=data[left++]; 

}

else

tmpArr[third++]=data[mid++]; 

//剩餘部分依次放入中間數組 

while

(mid<=right){ 

tmpArr[third++]=data[mid++]; 

while

(left<=center){ 

tmpArr[third++]=data[left++]; 

//将中間數組中的内容複制回原數組 

while

(tmp<=right){ 

data[tmp]=tmpArr[tmp++]; 

System.out.println(Arrays.toString(data)); 

}

8、基數排序

(1)基本思想:将所有待比較數值(正整數)統一為同樣的數位長度,數位較短的數前面補零。然後,從最低位開始,依次進行一次排序。這樣從最低位排序一直到最高位排序完成以後,數列就變成一個有序序列。

(2)執行個體:

常用排序算法/程式員必須掌握的8大排序算法

(3)用java實作

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

import

java.util.ArrayList; 

import

java.util.List; 

public

class

radixSort { 

inta[]={

49

,

38

,

65

,

97

,

76

,

13

,

27

,

49

,

78

,

34

,

12

,

64

,

5

,

4

,

62

,

99

,

98

,

54

,

101

,

56

,

17

,

18

,

23

,

34

,

15

,

35

,

25

,

53

,

51

}; 

public

radixSort(){ 

sort(a); 

for

(inti=

;i<a.length;i++){ 

System.out.println(a[i]); 

}        

public

void

sort(

int

[] array){   

//首先确定排序的趟數;   

int

max=array[

];   

for

(inti=

1

;i<array.length;i++){   

if

(array[i]>max){   

max=array[i];   

}   

}   

int

time=

;   

//判斷位數;   

while

(max>

){   

max/=

10

;   

time++;   

}   

//建立10個隊列;   

List<ArrayList> queue=newArrayList<ArrayList>();   

for

(

int

i=

;i<

10

;i++){   

ArrayList<Integer>queue1=

new

ArrayList<Integer>(); 

queue.add(queue1);   

}   

//進行time次配置設定和收集;   

for

(

int

i=

;i<time;i++){   

//配置設定數組元素;   

for

(intj=

;j<array.length;j++){   

//得到數字的第time+1位數; 

int

x=array[j]%(

int

)Math.pow(

10

,i+

1

)/(

int

)Math.pow(

10

, i); 

ArrayList<Integer>queue2=queue.get(x); 

queue2.add(array[j]); 

queue.set(x, queue2); 

}  

int

count=

;

//元素計數器;   

//收集隊列元素;   

for

(

int

k=

;k<

10

;k++){ 

while

(queue.get(k).size()>

){ 

ArrayList<Integer>queue3=queue.get(k); 

array[count]=queue3.get(

);   

queue3.remove(

); 

count++; 

}  

}   

}              

}