数据结构-归并排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构-归并排序相关的知识,希望对你有一定的参考价值。

(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]。

技术分享

(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<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);
	}
}

排序结果如下:

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



以上是关于数据结构-归并排序的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript算法(归并排序与快速排序)

堆排/快排/希尔/归并排序,谁更快?

快速排序和归并排序的区别,有代码

归并排序和冒泡排序

算法与数据结构:时间复杂度——以归并排序为例

经典排序算法之归并排序