diff --git a/package.json b/package.json index c0495f29..b3a21669 100644 --- a/package.json +++ b/package.json @@ -19,4 +19,3 @@ "author": "TheAlgorithms", "license": "MIT" } - \ No newline at end of file diff --git a/sorts/merge_sort.ts b/sorts/merge_sort.ts index 6a719226..a4f164ef 100644 --- a/sorts/merge_sort.ts +++ b/sorts/merge_sort.ts @@ -5,52 +5,45 @@ * @example MergeSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] * @Complexity_Analysis * Space complexity - O(n) - * Time complexity + * Time complexity * Best case - O(nlogn) * Worst case - O(nlogn) * Average case - O(nlogn) - * - * Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation. - * T(n) = 2T(n/2) + O(n) + * + * Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation. + * T(n) = 2T(n/2) + O(n) * The solution of the above recurrence is O(nLogn). */ - export const MergeSort = (items: number[]): number[] => { - var halfLength = Math.ceil(items.length / 2); - var low = items.slice(0, halfLength); - var high = items.slice(halfLength); - if (halfLength > 1) { - low = MergeSort(low); - high = MergeSort(high); - } - return merge(low, high); -}; +export function mergeSort(array: number[]): number[] { + if (array.length <= 1) return array.slice() -export const merge = (low: number[], high: number[]): number[] => { - let indexLow = 0; - let indexHigh = 0; - let curIndex = 0; - let merged = Array(low.length + high.length); + const midIndex = Math.floor(array.length / 2) + const left = array.slice(0, midIndex) + const right = array.slice(midIndex, array.length) - while (indexLow < low.length && indexHigh < high.length) { + return merge(mergeSort(left), mergeSort(right)) +} - if (low[indexLow] <= high[indexHigh]) { - merged[curIndex++] = low[indexLow]; - indexLow++; - } else { - merged[curIndex++] = high[indexHigh]; - indexHigh++; - } - } +function merge(left: number[], right: number[]): number[] { + const result = Array(left.length + right.length) + let curIndex = 0 + let leftIndex = 0 + let rightIndex = 0 - while (indexLow < low.length) { - merged[curIndex++] = low[indexLow]; - indexLow++; + while (leftIndex < left.length && rightIndex < right.length) { + if (left[leftIndex] < right[rightIndex]) { + result[curIndex++] = left[leftIndex++] + } else { + result[curIndex++] = right[rightIndex++] } + } + while (leftIndex < left.length) { + result[curIndex++] = left[leftIndex++] + } + while (rightIndex < right.length) { + result[curIndex++] = right[rightIndex++] + } - while (indexHigh < high.length) { - merged[curIndex++] = high[indexHigh]; - indexHigh++; - } - return merged; -}; + return result +} diff --git a/sorts/test/merge_sort.test.ts b/sorts/test/merge_sort.test.ts index 8b85c712..5b6f81fc 100644 --- a/sorts/test/merge_sort.test.ts +++ b/sorts/test/merge_sort.test.ts @@ -1,19 +1,18 @@ -import { MergeSort } from "../merge_sort"; +import { mergeSort } from "../merge_sort" describe("Merge Sort", () => { - it("generating array with variable length and comparing with sorted array", () => { - let arrLenArr = [10, 200, 40000]; + it("generating array with variable length and comparing with sorted array", () => { + let arrLenArr = [10, 200, 40000] - arrLenArr.forEach((arrLen: number) => { - - let inBuiltSortArr = Array(arrLen) - for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() * 10000 } - let mergeSortArray = inBuiltSortArr.slice(); - - inBuiltSortArr.sort((a, b) => a - b); - expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); - - }) - }); -}); + arrLenArr.forEach((arrLen: number) => { + let inBuiltSortArr = Array(arrLen) + for (let i = 0; i < arrLen; i++) { + inBuiltSortArr[i] = Math.random() * 10000 + } + let mergeSortArray = inBuiltSortArr.slice() + inBuiltSortArr.sort((a, b) => a - b) + expect(mergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr) + }) + }) +})