From 7521575b6e0626a972156813716bff71eac967a5 Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 12:04:34 +0530 Subject: [PATCH 1/5] Added merge sort --- Sorts/MergeSort.ts | 49 ++++++++++++++++++++++++++++++++++++ Sorts/test/MergeSort.test.ts | 20 +++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 Sorts/MergeSort.ts create mode 100644 Sorts/test/MergeSort.test.ts diff --git a/Sorts/MergeSort.ts b/Sorts/MergeSort.ts new file mode 100644 index 00000000..d2a09e2a --- /dev/null +++ b/Sorts/MergeSort.ts @@ -0,0 +1,49 @@ +/** + * @function mergeSort + * @description keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is sorted. + * @see [Merge Sort](https://www.javatpoint.com/merge-sort) + * @example MergeSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] + */ + + 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 const merge = (low: number[], high: number[]): number[] => { + let indexLow = 0; + let indexHigh = 0; + let lengthLow = low.length; + let lengthHigh = high.length; + let merged : number[] = []; + while (indexLow < lengthLow || indexHigh < lengthHigh) { + let lowItem = low[indexLow]; + let highItem = high[indexHigh]; + if (lowItem !== undefined) { + if (highItem === undefined) { + merged.push(lowItem); + indexLow++; + } else { + if (lowItem <= highItem) { + merged.push(lowItem); + indexLow++; + } else { + merged.push(highItem); + indexHigh++; + } + } + } else { + if (highItem !== undefined) { + merged.push(highItem); + indexHigh++; + } + } + } + return merged; +} \ No newline at end of file diff --git a/Sorts/test/MergeSort.test.ts b/Sorts/test/MergeSort.test.ts new file mode 100644 index 00000000..80705a14 --- /dev/null +++ b/Sorts/test/MergeSort.test.ts @@ -0,0 +1,20 @@ +import { MergeSort } from "../MergeSort"; + +/** + * Space complexity - O(n) + * 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) + * The solution of the above recurrence is O(nLogn). + */ + +describe("Merge Sort", () => { + it("should return the correct value", () => { + expect(MergeSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + }); + + }); \ No newline at end of file From 053519d79af1059af32560358d1fc702e96e702b Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 16:37:38 +0530 Subject: [PATCH 2/5] Refactored code --- Sorts/MergeSort.ts | 57 ++++++++++++++++++++---------------- Sorts/test/MergeSort.test.ts | 24 +++++---------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/Sorts/MergeSort.ts b/Sorts/MergeSort.ts index d2a09e2a..6a719226 100644 --- a/Sorts/MergeSort.ts +++ b/Sorts/MergeSort.ts @@ -3,6 +3,16 @@ * @description keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is sorted. * @see [Merge Sort](https://www.javatpoint.com/merge-sort) * @example MergeSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] + * @Complexity_Analysis + * Space complexity - O(n) + * 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) + * The solution of the above recurrence is O(nLogn). */ export const MergeSort = (items: number[]): number[] => { @@ -14,36 +24,33 @@ high = MergeSort(high); } return merge(low, high); -} +}; export const merge = (low: number[], high: number[]): number[] => { let indexLow = 0; let indexHigh = 0; - let lengthLow = low.length; - let lengthHigh = high.length; - let merged : number[] = []; - while (indexLow < lengthLow || indexHigh < lengthHigh) { - let lowItem = low[indexLow]; - let highItem = high[indexHigh]; - if (lowItem !== undefined) { - if (highItem === undefined) { - merged.push(lowItem); - indexLow++; - } else { - if (lowItem <= highItem) { - merged.push(lowItem); - indexLow++; - } else { - merged.push(highItem); - indexHigh++; - } - } + let curIndex = 0; + let merged = Array(low.length + high.length); + + while (indexLow < low.length && indexHigh < high.length) { + + if (low[indexLow] <= high[indexHigh]) { + merged[curIndex++] = low[indexLow]; + indexLow++; } else { - if (highItem !== undefined) { - merged.push(highItem); - indexHigh++; - } + merged[curIndex++] = high[indexHigh]; + indexHigh++; } } + + while (indexLow < low.length) { + merged[curIndex++] = low[indexLow]; + indexLow++; + } + + while (indexHigh < high.length) { + merged[curIndex++] = high[indexHigh]; + indexHigh++; + } return merged; -} \ No newline at end of file +}; diff --git a/Sorts/test/MergeSort.test.ts b/Sorts/test/MergeSort.test.ts index 80705a14..13df0dd0 100644 --- a/Sorts/test/MergeSort.test.ts +++ b/Sorts/test/MergeSort.test.ts @@ -1,20 +1,12 @@ import { MergeSort } from "../MergeSort"; -/** - * Space complexity - O(n) - * 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) - * The solution of the above recurrence is O(nLogn). - */ - describe("Merge Sort", () => { - it("should return the correct value", () => { - expect(MergeSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); - }); + it("generating array and comparing with sorted array", () => { + let arrLen = 10; + let inBuiltSortArr = Array.from(Array(arrLen)).map(x=>Math.random()); + let mergeSortArray = inBuiltSortArr; - }); \ No newline at end of file + inBuiltSortArr.sort((a,b) => a-b); + expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); + }); +}); \ No newline at end of file From e9d42068ea256b4a5f4e5d2698b5a076e139883e Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 17:18:21 +0530 Subject: [PATCH 3/5] Refactored code --- Sorts/test/MergeSort.test.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Sorts/test/MergeSort.test.ts b/Sorts/test/MergeSort.test.ts index 13df0dd0..d228d4f7 100644 --- a/Sorts/test/MergeSort.test.ts +++ b/Sorts/test/MergeSort.test.ts @@ -1,12 +1,25 @@ import { MergeSort } from "../MergeSort"; describe("Merge Sort", () => { - it("generating array and comparing with sorted array", () => { + it("generating array with length 10 and comparing with sorted array", () => { let arrLen = 10; - let inBuiltSortArr = Array.from(Array(arrLen)).map(x=>Math.random()); + + let inBuiltSortArr = Array(arrLen) + for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() } + let mergeSortArray = inBuiltSortArr; + + inBuiltSortArr.sort((a, b) => a - b); + expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); + }); + + it("generating array with length 20000 and comparing with sorted array", () => { + let arrLen = 20000; + + let inBuiltSortArr = Array(arrLen) + for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() } let mergeSortArray = inBuiltSortArr; - inBuiltSortArr.sort((a,b) => a-b); + inBuiltSortArr.sort((a, b) => a - b); expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); }); }); \ No newline at end of file From d4f69d7a6944665e4ebc48227f220c5344232092 Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 17:53:46 +0530 Subject: [PATCH 4/5] Updated test file --- Sorts/test/MergeSort.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sorts/test/MergeSort.test.ts b/Sorts/test/MergeSort.test.ts index d228d4f7..6fe25731 100644 --- a/Sorts/test/MergeSort.test.ts +++ b/Sorts/test/MergeSort.test.ts @@ -6,7 +6,7 @@ describe("Merge Sort", () => { let inBuiltSortArr = Array(arrLen) for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() } - let mergeSortArray = inBuiltSortArr; + let mergeSortArray = inBuiltSortArr.slice(); inBuiltSortArr.sort((a, b) => a - b); expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); @@ -17,7 +17,7 @@ describe("Merge Sort", () => { let inBuiltSortArr = Array(arrLen) for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() } - let mergeSortArray = inBuiltSortArr; + let mergeSortArray = inBuiltSortArr.slice(); inBuiltSortArr.sort((a, b) => a - b); expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); From 490682ee57c7a697df055bd405539793f587ce18 Mon Sep 17 00:00:00 2001 From: Rajat Garg Date: Sat, 8 Oct 2022 18:22:30 +0530 Subject: [PATCH 5/5] Modified test file --- Sorts/test/MergeSort.test.ts | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/Sorts/test/MergeSort.test.ts b/Sorts/test/MergeSort.test.ts index 6fe25731..52665329 100644 --- a/Sorts/test/MergeSort.test.ts +++ b/Sorts/test/MergeSort.test.ts @@ -1,25 +1,19 @@ import { MergeSort } from "../MergeSort"; describe("Merge Sort", () => { - it("generating array with length 10 and comparing with sorted array", () => { - let arrLen = 10; + it("generating array with variable length and comparing with sorted array", () => { + let arrLenArr = [10, 200, 40000]; - let inBuiltSortArr = Array(arrLen) - for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() } - let mergeSortArray = inBuiltSortArr.slice(); + arrLenArr.forEach((arrLen: number) => { - inBuiltSortArr.sort((a, b) => a - b); - expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); - }); - - it("generating array with length 20000 and comparing with sorted array", () => { - let arrLen = 20000; + let inBuiltSortArr = Array(arrLen) + for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() * 10000 } + let mergeSortArray = inBuiltSortArr.slice(); - let inBuiltSortArr = Array(arrLen) - for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() } - let mergeSortArray = inBuiltSortArr.slice(); + inBuiltSortArr.sort((a, b) => a - b); + expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); - inBuiltSortArr.sort((a, b) => a - b); - expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); + }) }); -}); \ No newline at end of file +}); +