(1)基本思想:归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并

归并过程为:比较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]。

数据结构-归并排序_第1张图片

(2)算法描述

归并操作的工作原理如下:

第一步:申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列;

第二步:设定两个指针,最初位置分别为两个已经排序序列的起始位置;

第三步:比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置;

重复步骤3直到某一指针超出序列尾,将另一序列剩下的所有元素直接复制到合并序列尾。

(3)代码如下:

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 
  

排序结果如下:

[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]