java程序員必知的8大排序
8種排序之間的關系:
1, 直接插入排序
(1)基本思想:在要排序的一組數中,假設前面(n-1)[n>=2] 個數已經是排
好順序的,現在要把第n個數插到前面的有序數中,使得這n個數
也是排好順序的。如此反復循環,直到全部排好順序。
(2)實例
(3)用java實現
01 |
package com.njue; |
02 |
|
03 |
public class insertSort { |
04 |
public insertSort(){ |
05 |
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 }; |
06 |
int temp= 0 ; |
07 |
for ( int i= 1 ;i<a.length;i++){ |
08 |
int j=i- 1 ; |
09 |
temp=a[i]; |
10 |
for (;j>= 0 &&temp<a[j];j--){ |
11 |
a[j+ 1 ]=a[j]; //將大于temp的值整體后移一個單位 |
12 |
} |
13 |
a[j+ 1 ]=temp; |
14 |
} |
15 |
for ( int i= 0 ;i<a.length;i++) |
16 |
System.out.println(a[i]); |
17 |
} |
18 |
} |
2, 希爾排序(最小增量排序)
(1)基本思想:算法先將要排序的一組數按某個增量d(n/2,n為要排序數的個數)分成若干組,每組中記錄的下標相差d.對每組中全部元素進行直接插入排序,然后再用一個較小的增量(d/2)對它進行分組,在每組中再進行直接插入排序。當增量減到1時,進行直接插入排序后,排序完成。
(2)實例:

(3)用java實現
01 |
public class shellSort { |
02 |
public shellSort(){ |
03 |
int a[]={ 1 , 54 , 6 , 3 , 78 , 34 , 12 , 45 , 56 , 100 }; |
04 |
double d1=a.length; |
05 |
int temp= 0 ; |
06 |
while ( true ){ |
07 |
d1= Math.ceil(d1/ 2 ); |
08 |
int d=( int ) d1; |
09 |
for ( int x= 0 ;x<d;x++){ |
10 |
for ( int i=x+d;i<a.length;i+=d){ |
11 |
int j=i-d; |
12 |
temp=a[i]; |
13 |
for (;j>= 0 &&temp<a[j];j-=d){ |
14 |
a[j+d]=a[j]; |
15 |
} |
16 |
a[j+d]=temp; |
17 |
} |
18 |
} |
19 |
if (d== 1 ) |
20 |
break ; |
21 |
} |
22 |
for ( int i= 0 ;i<a.length;i++) |
23 |
System.out.println(a[i]); |
24 |
} |
25 |
} |
3.簡單選擇排序
(1)基本思想:在要排序的一組數中,選出最小的一個數與第一個位置的數交換;
然后在剩下的數當中再找最小的與第二個位置的數交換,如此循環到倒數第二個數和最后一個數比較為止。
(2)實例:

(3)用java實現
01 |
public class selectSort { |
02 |
public selectSort(){ |
03 |
int a[]={ 1 , 54 , 6 , 3 , 78 , 34 , 12 , 45 }; |
04 |
int position= 0 ; |
05 |
for ( int i= 0 ;i<a.length;i++){ |
06 |
|
07 |
int j=i+ 1 ; |
08 |
position=i; |
09 |
int temp=a[i]; |
10 |
for (;j<a.length;j++){ |
11 |
if (a[j]<temp){ |
12 |
temp=a[j]; |
13 |
position=j; |
14 |
} |
15 |
} |
16 |
a[position]=a[i]; |
17 |
a[i]=temp; |
18 |
} |
19 |
for ( int i= 0 ;i<a.length;i++) |
20 |
System.out.println(a[i]); |
21 |
} |
22 |
} |
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
建堆:

交換,從堆中踢出最大數
依次類推:最后堆中剩余的最后兩個結點交換,踢出一個,排序完成。
(3)用java實現
01 |
import java.util.Arrays; |
02 |
03 |
public class HeapSort { |
04 |
int a[]={ 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 }; |
05 |
public HeapSort(){ |
06 |
heapSort(a); |
07 |
} |
08 |
public void heapSort( int [] a){ |
09 |
System.out.println( "開始排序" ); |
10 |
int arrayLength=a.length; |
11 |
//循環建堆 |
12 |
for ( int i= 0 ;i<arrayLength- 1 ;i++){ |
13 |
//建堆 |
14 |
15 |
buildMaxHeap(a,arrayLength- 1 -i); |
16 |
//交換堆頂和最后一個元素 |
17 |
swap(a, 0 ,arrayLength- 1 -i); |
18 |
System.out.println(Arrays.toString(a)); |
19 |
} |
20 |
} |
21 |
22 |
private void swap( int [] data, int i, int j) { |
23 |
// TODO Auto-generated method stub |
24 |
int tmp=data[i]; |
25 |
data[i]=data[j]; |
26 |
data[j]=tmp; |
27 |
} |
28 |
//對data數組從0到lastIndex建大頂堆 |
29 |
private void buildMaxHeap( int [] data, int lastIndex) { |
30 |
// TODO Auto-generated method stub |
31 |
//從lastIndex處節點(最后一個節點)的父節點開始 |
32 |
for ( int i=(lastIndex- 1 )/ 2 ;i>= 0 ;i--){ |
33 |
//k保存正在判斷的節點 |
34 |
int k=i; |
35 |
//如果當前k節點的子節點存在 |
36 |
while (k* 2 + 1 <=lastIndex){ |
37 |
//k節點的左子節點的索引 |
38 |
int biggerIndex= 2 *k+ 1 ; |
39 |
//如果biggerIndex小于lastIndex,即biggerIndex+1代表的k節點的右子節點存在 |
40 |
if (biggerIndex<lastIndex){ |
41 |
//若果右子節點的值較大 |
42 |
if (data[biggerIndex]<data[biggerIndex+ 1 ]){ |
43 |
//biggerIndex總是記錄較大子節點的索引 |
44 |
biggerIndex++; |
45 |
} |
46 |
} |
47 |
//如果k節點的值小于其較大的子節點的值 |
48 |
if (data[k]<data[biggerIndex]){ |
49 |
//交換他們 |
50 |
swap(data,k,biggerIndex); |
51 |
//將biggerIndex賦予k,開始while循環的下一次循環,重新保證k節點的值大于其左右子節點的值 |
52 |
k=biggerIndex; |
53 |
} else { |
54 |
break ; |
55 |
} |
56 |
}<p align= "left" > <span> </span>}</p> |
57 |
<p align= "left" > }</p> |
58 |
<p align= "left" > <span style= "background-color:white;" >}</span></p> |
5.冒泡排序
(1)基本思想:在要排序的一組數中,對當前還未排好序的范圍內的全部數,自上而下對相鄰的兩個數依次進行比較和調整,讓較大的數往下沉,較小的往上冒。即:每當兩相鄰的數比較后發現它們的排序與排序要求相反時,就將它們互換。
(2)實例:

(3)用java實現
01 |
public class bubbleSort { |
02 |
public bubbleSort(){ |
03 |
int a[]={ 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 }; |
04 |
int temp= 0 ; |
05 |
for ( int i= 0 ;i<a.length- 1 ;i++){ |
06 |
for ( int j= 0 ;j<a.length- 1 -i;j++){ |
07 |
if (a[j]>a[j+ 1 ]){ |
08 |
temp=a[j]; |
09 |
a[j]=a[j+ 1 ]; |
10 |
a[j+ 1 ]=temp; |
11 |
} |
12 |
} |
13 |
} |
14 |
for ( int i= 0 ;i<a.length;i++) |
15 |
System.out.println(a[i]); |
16 |
} |
17 |
} |
6.快速排序
(1)基本思想:選擇一個基準元素,通常選擇第一個元素或者最后一個元素,通過一趟掃描,將待排序列分成兩部分,一部分比基準元素小,一部分大于等于基準元素,此時基準元素在其排好序后的正確位置,然后再用同樣的方法遞歸地排序劃分的兩部分。
(2)實例:
(3)用java實現
01 |
public class quickSort { |
02 |
int a[]={ 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 }; |
03 |
public quickSort(){ |
04 |
quick(a); |
05 |
for ( int i= 0 ;i<a.length;i++) |
06 |
System.out.println(a[i]); |
07 |
} |
08 |
public int getMiddle( int [] list, int low, int high) { |
09 |
int tmp = list[low]; //數組的第一個作為中軸 |
10 |
while (low < high) { |
11 |
while (low < high && list[high] >= tmp) { |
12 |
13 |
high--; |
14 |
} |
15 |
list[low] = list[high]; //比中軸小的記錄移到低端 |
16 |
while (low < high && list[low] <= tmp) { |
17 |
low++; |
18 |
} |
19 |
list[high] = list[low]; //比中軸大的記錄移到高端 |
20 |
} |
21 |
list[low] = tmp; //中軸記錄到尾 |
22 |
return low; //返回中軸的位置 |
23 |
} |
24 |
public void _quickSort( int [] list, int low, int high) { |
25 |
if (low < high) { |
26 |
int middle = getMiddle(list, low, high); //將list數組進行一分為二 |
27 |
_quickSort(list, low, middle - 1 ); //對低字表進行遞歸排序 |
28 |
_quickSort(list, middle + 1 , high); //對高字表進行遞歸排序 |
29 |
} |
30 |
} |
31 |
public void quick( int [] a2) { |
32 |
if (a2.length > 0 ) { //查看數組是否為空 |
33 |
_quickSort(a2, 0 , a2.length - 1 ); |
34 |
} |
35 |
} |
36 |
} |
7、歸并排序
(1)基本排序:歸并(Merge)排序法是將兩個(或兩個以上)有序表合并成一個新的有序表,即把待排序序列分為若干個子序列,每個子序列是有序的。然后再把有序子序列合并為整體有序序列。
(2)實例:

(3)用java實現
01 |
import java.util.Arrays; |
02 |
03 |
public class mergingSort { |
04 |
int a[]={ 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 }; |
05 |
public mergingSort(){ |
06 |
sort(a, 0 ,a.length- 1 ); |
07 |
for ( int i= 0 ;i<a.length;i++) |
08 |
System.out.println(a[i]); |
09 |
} |
10 |
public void sort( int [] data, int left, int right) { |
11 |
// TODO Auto-generated method stub |
12 |
if (left<right){ |
13 |
//找出中間索引 |
14 |
int center=(left+right)/ 2 ; |
15 |
//對左邊數組進行遞歸 |
16 |
sort(data,left,center); |
17 |
//對右邊數組進行遞歸 |
18 |
sort(data,center+ 1 ,right); |
19 |
//合并 |
20 |
merge(data,left,center,right); |
21 |
|
22 |
} |
23 |
} |
24 |
public void merge( int [] data, int left, int center, int right) { |
25 |
// TODO Auto-generated method stub |
26 |
int [] tmpArr= new int [data.length]; |
27 |
int mid=center+ 1 ; |
28 |
//third記錄中間數組的索引 |
29 |
int third=left; |
30 |
int tmp=left; |
31 |
while (left<=center&&mid<=right){ |
32 |
33 |
//從兩個數組中取出最小的放入中間數組 |
34 |
if (data[left]<=data[mid]){ |
35 |
tmpArr[third++]=data[left++]; |
36 |
} else { |
37 |
tmpArr[third++]=data[mid++]; |
38 |
} |
39 |
} |
40 |
//剩余部分依次放入中間數組 |
41 |
while (mid<=right){ |
42 |
tmpArr[third++]=data[mid++]; |
43 |
} |
44 |
while (left<=center){ |
45 |
tmpArr[third++]=data[left++]; |
46 |
} |
47 |
//將中間數組中的內容復制回原數組 |
48 |
while (tmp<=right){ |
49 |
data[tmp]=tmpArr[tmp++]; |
50 |
} |
51 |
System.out.println(Arrays.toString(data)); |
52 |
} |
53 |
54 |
} |
8、基數排序
(1)基本思想:將所有待比較數值(正整數)統一為同樣的數位長度,數位較短的數前面補零。然后,從最低位開始,依次進行一次排序。這樣從最低位排序一直到最高位排序完成以后,數列就變成一個有序序列。
(2)實例:

(3)用java實現
01 |
import java.util.ArrayList; |
02 |
import java.util.List; |
03 |
04 |
public class radixSort { |
05 |
int a[]={ 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 }; |
06 |
public radixSort(){ |
07 |
sort(a); |
08 |
for ( int i= 0 ;i<a.length;i++) |
09 |
System.out.println(a[i]); |
10 |
} |
11 |
public void sort( int [] array){ |
12 |
|
13 |
//首先確定排序的趟數; |
14 |
int max=array[ 0 ]; |
15 |
for ( int i= 1 ;i<array.length;i++){ |
16 |
if (array[i]>max){ |
17 |
max=array[i]; |
18 |
} |
19 |
} |
20 |
21 |
int time= 0 ; |
22 |
//判斷位數; |
23 |
while (max> 0 ){ |
24 |
max/= 10 ; |
25 |
time++; |
26 |
} |
27 |
|
28 |
//建立10個隊列; |
29 |
List<ArrayList> queue= new ArrayList<ArrayList>(); |
30 |
for ( int i= 0 ;i< 10 ;i++){ |
31 |
ArrayList<Integer> queue1= new ArrayList<Integer>(); |
32 |
queue.add(queue1); |
33 |
} |
34 |
|
35 |
//進行time次分配和收集; |
36 |
for ( int i= 0 ;i<time;i++){ |
37 |
|
38 |
//分配數組元素; |
39 |
for ( int j= 0 ;j<array.length;j++){ |
40 |
//得到數字的第time+1位數; |
41 |
int x=array[j]%( int )Math.pow( 10 , i+ 1 )/( int )Math.pow( 10 , i); |
42 |
ArrayList<Integer> queue2=queue.get(x); |
43 |
queue2.add(array[j]); |
44 |
queue.set(x, queue2); |
45 |
} |
46 |
int count= 0 ; //元素計數器; |
47 |
//收集隊列元素; |
48 |
for ( int k= 0 ;k< 10 ;k++){ |
49 |
while (queue.get(k).size()> 0 ){ |
50 |
ArrayList<Integer> queue3=queue.get(k); |
51 |
array[count]=queue3.get( 0 ); |
52 |
queue3.remove( 0 ); |
53 |
count++; |
54 |
} |
55 |
} |
56 |
} |
57 |
|
58 |
} |
59 |
60 |
} |