(1)基本思想:歸併(Merge)排序法是將兩個(或兩個以上)有序表合併成一個新的有序表,即把待排序序列分爲若干個子序列,每一個子序列是有序的。而後再把有序子序列合併爲總體有序序列。歸併排序(MERGE-SORT)是創建在歸併操做上的一種有效的排序算法,該算法是採用分治法(Divide and Conquer)的一個很是典型的應用。將已有序的子序列合併,獲得徹底有序的序列;即先使每一個子序列有序,再使子序列段間有序。若將兩個有序表合併成一個有序表,稱爲二路歸併。java
歸併過程爲:比較a[i]和a[j]的大小,若a[i]≤a[j],則將第一個有序表中的元素a[i]複製到r[k]中,並令i和k分別加上1;不然將第二個有序表中的元素a[j]複製到r[k]中,並令j和k分別加上1,如此循環下去,直到其中一個有序表取完,而後再將另外一個有序表中剩餘的元素複製到r中從下標k到下標t的單元。歸併排序的算法咱們一般用遞歸實現,先把待排序區間[s,t]以中點二分,接着把左邊子區間排序,再把右邊子區間排序,最後把左區間和右區間用一次歸併操做合併成有序的區間[s,t]。算法
(2)算法描述
數組
歸併操做的工做原理以下:ide
第一步:申請空間,使其大小爲兩個已經排序序列之和,該空間用來存放合併後的序列;spa
第二步:設定兩個指針,最初位置分別爲兩個已經排序序列的起始位置;3d
第三步:比較兩個指針所指向的元素,選擇相對小的元素放入到合併空間,並移動指針到下一位置;指針
重複步驟3直到某一指針超出序列尾,將另外一序列剩下的全部元素直接複製到合併序列尾。orm
(3)代碼以下:htm
package structsmethod; import java.util.Arrays; public class mergingSort { /* * 歸併排序 */ 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}; public mergingSort(){ sort(a,0,a.length-1); for(int i=0;i<a.length;i++) System.out.println(a[i]); } public void sort(int[] data, int left, int right) { if(left<right){ //找出中間索引 int center=(left+right)/2; //對左邊數組進行遞歸 sort(data,left,center); //對右邊數組進行遞歸 sort(data,center+1,right); //合併 merge(data,left,center,right); } } public void merge(int[] data, int left, int center, int right) { int [] tmpArr=new int[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)); } public static void main(String[] args) { mergingSort ms=new mergingSort(); ms.sort(ms.a, 0, ms.a.length-1); } }
排序結果以下:
blog
[38, 49, 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]
[38, 49, 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]
[38, 49, 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]
[38, 49, 65, 97, 13, 76, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[38, 49, 65, 97, 13, 27, 76, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 49, 78, 12, 34, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 12, 34, 49, 78, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 12, 34, 49, 78, 5, 64, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 12, 34, 49, 78, 4, 5, 64, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[13, 27, 38, 49, 65, 76, 97, 4, 5, 12, 34, 49, 64, 78, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 62, 99, 54, 98, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 54, 62, 98, 99, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 54, 62, 98, 99, 17, 56, 18, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 54, 62, 98, 99, 17, 18, 56, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 23, 34, 15, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 15, 23, 34, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 15, 23, 34, 35, 25, 53, 51]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 15, 23, 34, 35, 25, 51, 53]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 17, 18, 54, 56, 62, 98, 99, 15, 23, 25, 34, 35, 51, 53]
[4, 5, 12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97, 15, 17, 18, 23, 25, 34, 35, 51, 53, 54, 56, 62, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
4
5
12
13
15
17
18
23
25
27
34
34
35
38
49
49
51
53
54
56
62
64
65
76
78
97
98
99
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]
[4, 5, 12, 13, 15, 17, 18, 23, 25, 27, 34, 34, 35, 38, 49, 49, 51, 53, 54, 56, 62, 64, 65, 76, 78, 97, 98, 99]