|
5 | 5 | * @example MergeSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8]
|
6 | 6 | * @Complexity_Analysis
|
7 | 7 | * Space complexity - O(n)
|
8 |
| - * Time complexity |
| 8 | + * Time complexity |
9 | 9 | * Best case - O(nlogn)
|
10 | 10 | * Worst case - O(nlogn)
|
11 | 11 | * Average case - O(nlogn)
|
12 |
| - * |
13 |
| - * Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation. |
14 |
| - * T(n) = 2T(n/2) + O(n) |
| 12 | + * |
| 13 | + * Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation. |
| 14 | + * T(n) = 2T(n/2) + O(n) |
15 | 15 | * The solution of the above recurrence is O(nLogn).
|
16 | 16 | */
|
17 | 17 |
|
18 |
| - export const MergeSort = (items: number[]): number[] => { |
19 |
| - var halfLength = Math.ceil(items.length / 2); |
20 |
| - var low = items.slice(0, halfLength); |
21 |
| - var high = items.slice(halfLength); |
22 |
| - if (halfLength > 1) { |
23 |
| - low = MergeSort(low); |
24 |
| - high = MergeSort(high); |
25 |
| - } |
26 |
| - return merge(low, high); |
27 |
| -}; |
| 18 | +export function mergeSort(array: number[]): number[] { |
| 19 | + if (array.length <= 1) return array.slice() |
28 | 20 |
|
29 |
| -export const merge = (low: number[], high: number[]): number[] => { |
30 |
| - let indexLow = 0; |
31 |
| - let indexHigh = 0; |
32 |
| - let curIndex = 0; |
33 |
| - let merged = Array<number>(low.length + high.length); |
| 21 | + const midIndex = Math.floor(array.length / 2) |
| 22 | + const left = array.slice(0, midIndex) |
| 23 | + const right = array.slice(midIndex, array.length) |
34 | 24 |
|
35 |
| - while (indexLow < low.length && indexHigh < high.length) { |
| 25 | + return merge(mergeSort(left), mergeSort(right)) |
| 26 | +} |
36 | 27 |
|
37 |
| - if (low[indexLow] <= high[indexHigh]) { |
38 |
| - merged[curIndex++] = low[indexLow]; |
39 |
| - indexLow++; |
40 |
| - } else { |
41 |
| - merged[curIndex++] = high[indexHigh]; |
42 |
| - indexHigh++; |
43 |
| - } |
44 |
| - } |
| 28 | +function merge(left: number[], right: number[]): number[] { |
| 29 | + const result = Array<number>(left.length + right.length) |
| 30 | + let curIndex = 0 |
| 31 | + let leftIndex = 0 |
| 32 | + let rightIndex = 0 |
45 | 33 |
|
46 |
| - while (indexLow < low.length) { |
47 |
| - merged[curIndex++] = low[indexLow]; |
48 |
| - indexLow++; |
| 34 | + while (leftIndex < left.length && rightIndex < right.length) { |
| 35 | + if (left[leftIndex] < right[rightIndex]) { |
| 36 | + result[curIndex++] = left[leftIndex++] |
| 37 | + } else { |
| 38 | + result[curIndex++] = right[rightIndex++] |
49 | 39 | }
|
| 40 | + } |
| 41 | + while (leftIndex < left.length) { |
| 42 | + result[curIndex++] = left[leftIndex++] |
| 43 | + } |
| 44 | + while (rightIndex < right.length) { |
| 45 | + result[curIndex++] = right[rightIndex++] |
| 46 | + } |
50 | 47 |
|
51 |
| - while (indexHigh < high.length) { |
52 |
| - merged[curIndex++] = high[indexHigh]; |
53 |
| - indexHigh++; |
54 |
| - } |
55 |
| - return merged; |
56 |
| -}; |
| 48 | + return result |
| 49 | +} |
0 commit comments