归并排序

package com.itheima;

import java.util.Arrays;

/**
* 归并排序:把两个或者两个以上的有序表合并成为一个新的有序表,也就是把带排序序列拆分成若干个序列,每个子序列都是有序的,再把有序子序列合并成为整体有序序列
* @author Administrator
*
*/
public class MergeSort {

/**
 * 归并排序
 */
public static void mergeSort(int a[]){
    sort(a, 0, a.length - 1);
    for (int i = 0; i < a.length; i++) {
        System.out.println(a[i]);
    }

}

/**
 * 排序(递归)
 * @param data
 * @param left
 * @param right
 */
public static 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);
    }
}

/**
 * 合并数据(算法的关键)
 * @param data
 * @param left
 * @param center
 * @param right
 */
public static void merge(int data[], int left ,int center, int right){
    // 复制一个和原来的数组一模一样大小的数组(作为第三方的数组)
    int tempArray[] = new int[data.length];

    int middle = center + 1;
    //记录中间数组的索引
    int third = left;
    int temp = left;
    //1.
    while (left <= center && middle <= right) {
        //从两个数组中取出小的放入中间数组中
        if (data[left] <= data[middle]) {
            tempArray[third++] = data[left++];
        } else {
            tempArray[third++] = data[middle++];
        }
    }
    //2.
    while (left <= center) {
        tempArray[third++] = data[left++];
    }
    //3.
    while (middle <= right) {
        tempArray[third++] = data[middle++];
    }

    //将中间数组的内容复制会原来的数组
    while (temp <= right) {
        data[temp] = tempArray[temp++];

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



public static void main(String[] args) {
    // TODO Auto-generated method stub
     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}; 
     mergeSort(a);

}

}
运行结果:
[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

你可能感兴趣的:(归并排序,合并)