美文网首页
O(n log n) - Merge Sort

O(n log n) - Merge Sort

作者: Super_Alan | 来源:发表于2018-03-31 07:16 被阅读0次

Merge Sort is based on the divide-and-conquer paradigm.

Merge Sort involves 3 steps:

  1. Divide the array into two or more subarrays
  2. Sort each subarray(Conquer)
  3. Merge them into one


    Merge Sort 例子

daima:

int[] buffer = null;

public void mergeSort(int[] array) {
    if (array == null || array.length == 0) {
        return;
    }
    buffer = new int[array.length];
    mergeSortHelper(array, 0, array.length - 1);
}

private void mergeSortHelper(int[] array, int startIndex, int endIndex) {
    if (startIndex >= endIndex) {
        return;
    }
    int midIndex = startIndex + (endIndex - startIndex)/2;
    mergeSortHelper(array, startIndex, midIndex);
    mergeSortHelper(array, midIndex + 1, endIndex);
    mergeParts(startIndex, midIndex, endIndex, array);
}

private void mergeParts(int startIndex, int midIndex, int endIndex, int[] array) {
    for (int i = startIndex; i <= endIndex; i++) {
        buffer[i] = array[i];
    }

    int leftIndex = startIndex;
    int rightIndex = midIndex + 1;
    int index = startIndex;
    while (leftIndex <= midIndex && rightIndex <= endIndex && index <= endIndex) {
        if (buffer[leftIndex] <= buffer[rightIndex]) {
            array[index] = buffer[leftIndex];
            leftIndex++;
        } else {
            array[index] = buffer[rightIndex];
            rightIndex++;
        }
        index++;
    }

    while (leftIndex <= midIndex) {
        array[index] = buffer[leftIndex];
        index++;
        leftIndex++;
    }

    while (rightIndex <= endIndex) {
        array[index] = buffer[rightIndex];
        index++;
        rightIndex++;
    }
}

Merge Sort 是 stable 的。
Runtime: Fixed O(n log n).

分析:每一层递归中,都需要对所有的 items 进行操作,O(n) 操作。递归层数为 O(log n)。

Space Complexity: O(n).

需要额外的 buffer array 来进行合并操作,否则时间复杂度会提高。

这里的解法是 top down,其实也可以使用 bottom up 来解。step 从 1, 2, 4, 8,... 直到 step >= array.length, 进行对应的合并。其时间复杂度和空间复杂度跟上面应该是一样的。

相关文章

网友评论

      本文标题:O(n log n) - Merge Sort

      本文链接:https://www.haomeiwen.com/subject/dnzbcftx.html