算法札记Android知识Android开发

常用排序算法总结4一一归并排序

2016-08-31  本文已影响158人  craneyuan

定义

归并排序(英语:Merge sort),是创建在归并操作上的一种有效的排序算法,效率为O(n log n)。

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

归并排序演示动画1

递归法步骤

原理如下(假设序列共有n个元素):

归并排序演示动画2

迭代法步骤

递归版实现(java)

/**
 *
 * @Title: mergeSort
 * @Description: 归并排序,递归版
 * @param: @param <E>
 * @param: @param m
 * @param: @return
 * @return: List<E>
 * @throws
 */
public static <E extends Comparable<? super E>> List<E> mergeSort(List<E> m)
{
    if (m.size() <= 1)
        return m;

    int middle = m.size() / 2;
    List<E> left = m.subList(0, middle);
    List<E> right = m.subList(middle, m.size());

    right = mergeSort(right);
    left = mergeSort(left);
    List<E> result = merge(left, right);

    return result;
}

public static <E extends Comparable<? super E>> List<E> merge(List<E> left,
        List<E> right)
{
    List<E> result = new ArrayList<E>();
    Iterator<E> it1 = left.iterator();
    Iterator<E> it2 = right.iterator();

    E x = it1.next();
    E y = it2.next();
    while (true) {
        // change the direction of this comparison to change the direction
        // of the sort
        if (x.compareTo(y) <= 0) {
            result.add(x);
            if (it1.hasNext()) {
                x = it1.next();
            } else {
                result.add(y);
                while (it2.hasNext()) {
                    result.add(it2.next());
                }
                break;
            }
        } else {
            result.add(y);
            if (it2.hasNext()) {
                y = it2.next();
            } else {
                result.add(x);
                while (it1.hasNext()) {
                    result.add(it1.next());
                }
                break;
            }
        }
    }
    return result;
}

迭代版实现(java)

/**
 *
 * @Title: mergeSort
 * @Description: 归并排序,迭代版
 * @param: @param nums
 * @return: void
 * @throws
 */
public static void mergeSort(int[] nums)
{
    int len = nums.length;
    int[] result = new int[len];
    int block, start;

    for (block = 1; block < len*2; block *= 2) {
        for (start = 0; start < len; start += 2 * block) {
            int low = start;
            int mid = (start + block) < len ? (start + block) : len;
            int high = (start + 2 * block) < len ? (start + 2 * block)
                       : len;
            // 两个块的起始下标及结束下标
            int start1 = low, end1 = mid;
            int start2 = mid, end2 = high;
            // 开始对两个block进行归并排序
            while (start1 < end1 && start2 < end2) {
                result[low++] = nums[start1] < nums[start2] ? nums[start1++]
                                : nums[start2++];
            }
            while (start1 < end1) {
                result[low++] = nums[start1++];
            }
            while (start2 < end2) {
                result[low++] = nums[start2++];
            }
        }
        int[] temp = nums;
        nums = result;
        result = temp;
    }
    result = nums;
}
上一篇下一篇

猜你喜欢

热点阅读