O(n log n) - Merge Sort

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


    O(n log n) - Merge Sort_第1张图片
    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)