java排序算法_006归并排序

package wzs.sort;

import java.util.Arrays;

//归并排序(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作。归并排序算法依赖归并操作。
public class mergingSort
{

    public static void main(String[] args)
    {
        int a[] =
        {
                10, 3, 5, 7, 9, 1, 4, 2, 6, 8
        };
        sort(a, 0, a.length - 1);
        System.out.println("\n排序后:" + Arrays.toString(a));
    }

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

    public static 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));
    }

}


你可能感兴趣的:(算法,算法,java算法,经典算法,java经典算法)