From 5d1973df2b59b42f3772e7a04507670c5f2a2325 Mon Sep 17 00:00:00 2001 From: Benben Weng <61361198+benben6515@users.noreply.github.com> Date: Fri, 28 Apr 2023 15:59:09 +0800 Subject: [PATCH 1/6] Update merge_sort.ts - use JavaScript ES6 syntax - use `let`, `const` instead of `var` - make it more "clean", I supposed --- sorts/merge_sort.ts | 62 ++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/sorts/merge_sort.ts b/sorts/merge_sort.ts index 6a719226..daedd45d 100644 --- a/sorts/merge_sort.ts +++ b/sorts/merge_sort.ts @@ -14,43 +14,29 @@ * T(n) = 2T(n/2) + O(n) * The solution of the above recurrence is O(nLogn). */ + +export function mergeSort(array: number[]): number[] { + if (array.length <= 1) return array + + const midIndex = Math.floor(array.length / 2) + const leftArray = array.slice(0, midIndex) + const rightArray = array.slice(midIndex, array.length) + + return merge(mergeSort(leftArray), mergeSort(rightArray)) +} - 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); +function merge (array1: number[], array2: number[]): number[] { + const result = [] + let index1 = 0 + let index2 = 0 + + while (index1 < array1.length && index2 < array2.length) { + if (array1[index1] < array2[index2]) { + result.push(array1[index1++]) + } else { + result.push(array2[index2++]) } - return merge(low, high); -}; - -export const merge = (low: number[], high: number[]): number[] => { - let indexLow = 0; - let indexHigh = 0; - 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 { - 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; -}; + } + + return [...result, ...array1.slice(index1), ...array2.slice(index2)] +} From b12c82e6ad0ed231e42ceea591dafca296e13917 Mon Sep 17 00:00:00 2001 From: benben Date: Fri, 28 Apr 2023 18:54:40 +0800 Subject: [PATCH 2/6] fix: merge_sort.ts and mroe - fix merge_sort with return copy of array - rename merge_sort.ts `MergeSrot` => `mergeSort` - rename quick_sort.ts `QuickSrot` => `quickSort` - rename merge_sort.test.ts `MergeSrot` => `mergeSort` - rename quick_sort.test.ts `QuickSrot` => `quickSort` --- sorts/merge_sort.ts | 13 +++++++------ sorts/quick_sort.ts | 10 +++++----- sorts/test/merge_sort.test.ts | 15 +++++++-------- sorts/test/quick_sort.test.ts | 10 +++++----- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/sorts/merge_sort.ts b/sorts/merge_sort.ts index daedd45d..7a3e9d38 100644 --- a/sorts/merge_sort.ts +++ b/sorts/merge_sort.ts @@ -14,10 +14,10 @@ * T(n) = 2T(n/2) + O(n) * The solution of the above recurrence is O(nLogn). */ - + export function mergeSort(array: number[]): number[] { - if (array.length <= 1) return array - + if (array.length <= 1) return array.slice() + const midIndex = Math.floor(array.length / 2) const leftArray = array.slice(0, midIndex) const rightArray = array.slice(midIndex, array.length) @@ -29,7 +29,7 @@ function merge (array1: number[], array2: number[]): number[] { const result = [] let index1 = 0 let index2 = 0 - + while (index1 < array1.length && index2 < array2.length) { if (array1[index1] < array2[index2]) { result.push(array1[index1++]) @@ -37,6 +37,7 @@ function merge (array1: number[], array2: number[]): number[] { result.push(array2[index2++]) } } - - return [...result, ...array1.slice(index1), ...array2.slice(index2)] + + return result.concat(array1.slice(index1)).concat(array2.slice(index2)) } + diff --git a/sorts/quick_sort.ts b/sorts/quick_sort.ts index c0fd192e..70cca0d5 100644 --- a/sorts/quick_sort.ts +++ b/sorts/quick_sort.ts @@ -2,7 +2,7 @@ * @function quickSort * @description is an algorithm based on divide and conquer approach in which an array is split into sub-arrays and these sub arrays are recursively sorted to get final array * @see [Quick Sort](https://www.javatpoint.com/quick-sort) - * @example QuickSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] + * @example quickSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] */ export const partition = ( @@ -34,7 +34,7 @@ export const partition = ( }; /** - * Quicksort implementation + * quicksort implementation * * @param {number[]} array * @param {number} [left=0] @@ -51,7 +51,7 @@ export const partition = ( * When the above two cases are not met */ -export const QuickSort = ( +export const quickSort = ( array: number[], left: number = 0, right: number = array.length - 1 @@ -62,11 +62,11 @@ export const QuickSort = ( index = partition(array, left, right); if (left < index - 1) { - QuickSort(array, left, index - 1); + quickSort(array, left, index - 1); } if (index < right) { - QuickSort(array, index, right); + quickSort(array, index, right); } } diff --git a/sorts/test/merge_sort.test.ts b/sorts/test/merge_sort.test.ts index 8b85c712..44a1dfab 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]; arrLenArr.forEach((arrLen: number) => { - - let inBuiltSortArr = Array(arrLen) - for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() * 10000 } + 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); - - }) + expect(mergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr); + }); }); }); - diff --git a/sorts/test/quick_sort.test.ts b/sorts/test/quick_sort.test.ts index aa4e1167..1ac954f5 100644 --- a/sorts/test/quick_sort.test.ts +++ b/sorts/test/quick_sort.test.ts @@ -1,15 +1,15 @@ -import { QuickSort } from "../quick_sort"; +import { quickSort } from "../quick_sort"; describe("Quick Sort", () => { it("should return the correct value for average case", () => { - expect(QuickSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + expect(quickSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); it("should return the correct value for worst case", () => { - expect(QuickSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + expect(quickSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); it("should return the correct value for best case", () => { - expect(QuickSort([1, 4, 2, 9, 5, 7, 3, 8, 10, 6])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + expect(quickSort([1, 4, 2, 9, 5, 7, 3, 8, 10, 6])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); - }); \ No newline at end of file + }); From 6e32cb4bff9deeee2c2964de669bf1b55990a0f6 Mon Sep 17 00:00:00 2001 From: benben Date: Sat, 29 Apr 2023 00:42:55 +0800 Subject: [PATCH 3/6] fix: update merge_sort.ts --- sorts/test/merge_sort.test.ts | 28 ++++++++++++++-------------- sorts/test/quick_sort.test.ts | 12 ++++++++++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/sorts/test/merge_sort.test.ts b/sorts/test/merge_sort.test.ts index 44a1dfab..5b6f81fc 100644 --- a/sorts/test/merge_sort.test.ts +++ b/sorts/test/merge_sort.test.ts @@ -1,18 +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(); + 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); - }); - }); -}); + inBuiltSortArr.sort((a, b) => a - b) + expect(mergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr) + }) + }) +}) diff --git a/sorts/test/quick_sort.test.ts b/sorts/test/quick_sort.test.ts index 1ac954f5..59efdad1 100644 --- a/sorts/test/quick_sort.test.ts +++ b/sorts/test/quick_sort.test.ts @@ -4,12 +4,20 @@ describe("Quick Sort", () => { it("should return the correct value for average case", () => { expect(quickSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); - + it("should return the correct value for worst case", () => { expect(quickSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); - + it("should return the correct value for best case", () => { expect(quickSort([1, 4, 2, 9, 5, 7, 3, 8, 10, 6])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); + + it("should return the correct value for 1 element", () => { + expect(quickSort([1])).toStrictEqual([1]); + }); + + it("should return the correct value for some negative elements", () => { + expect(quickSort([3.14, 0, -2, 10])).toStrictEqual([-2, 0, 3.14, 10]); + }); }); From 0b71410400a8e9a914ac89715cef4755ba59c30f Mon Sep 17 00:00:00 2001 From: benben Date: Sat, 29 Apr 2023 00:45:27 +0800 Subject: [PATCH 4/6] fix: update merge_sort.ts --- package.json | 1 - sorts/merge_sort.ts | 42 ++++++++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 19 deletions(-) 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 7a3e9d38..a4f164ef 100644 --- a/sorts/merge_sort.ts +++ b/sorts/merge_sort.ts @@ -5,13 +5,13 @@ * @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). */ @@ -19,25 +19,31 @@ export function mergeSort(array: number[]): number[] { if (array.length <= 1) return array.slice() const midIndex = Math.floor(array.length / 2) - const leftArray = array.slice(0, midIndex) - const rightArray = array.slice(midIndex, array.length) - - return merge(mergeSort(leftArray), mergeSort(rightArray)) + const left = array.slice(0, midIndex) + const right = array.slice(midIndex, array.length) + + return merge(mergeSort(left), mergeSort(right)) } -function merge (array1: number[], array2: number[]): number[] { - const result = [] - let index1 = 0 - let index2 = 0 +function merge(left: number[], right: number[]): number[] { + const result = Array(left.length + right.length) + let curIndex = 0 + let leftIndex = 0 + let rightIndex = 0 - while (index1 < array1.length && index2 < array2.length) { - if (array1[index1] < array2[index2]) { - result.push(array1[index1++]) + while (leftIndex < left.length && rightIndex < right.length) { + if (left[leftIndex] < right[rightIndex]) { + result[curIndex++] = left[leftIndex++] } else { - result.push(array2[index2++]) + result[curIndex++] = right[rightIndex++] } } + while (leftIndex < left.length) { + result[curIndex++] = left[leftIndex++] + } + while (rightIndex < right.length) { + result[curIndex++] = right[rightIndex++] + } - return result.concat(array1.slice(index1)).concat(array2.slice(index2)) + return result } - From de6c7517e6b621283802db01d81af0d97392bc9d Mon Sep 17 00:00:00 2001 From: benben Date: Sat, 29 Apr 2023 01:39:30 +0800 Subject: [PATCH 5/6] fix: convert naming of quick_sort.ts --- sorts/test/quick_sort.test.ts | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/sorts/test/quick_sort.test.ts b/sorts/test/quick_sort.test.ts index 59efdad1..aa4e1167 100644 --- a/sorts/test/quick_sort.test.ts +++ b/sorts/test/quick_sort.test.ts @@ -1,23 +1,15 @@ -import { quickSort } from "../quick_sort"; +import { QuickSort } from "../quick_sort"; describe("Quick Sort", () => { it("should return the correct value for average case", () => { - expect(quickSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + expect(QuickSort([1, 4, 2, 5, 9, 6, 3, 8, 10, 7])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); - + it("should return the correct value for worst case", () => { - expect(quickSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + expect(QuickSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); - + it("should return the correct value for best case", () => { - expect(quickSort([1, 4, 2, 9, 5, 7, 3, 8, 10, 6])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); - }); - - it("should return the correct value for 1 element", () => { - expect(quickSort([1])).toStrictEqual([1]); - }); - - it("should return the correct value for some negative elements", () => { - expect(quickSort([3.14, 0, -2, 10])).toStrictEqual([-2, 0, 3.14, 10]); + expect(QuickSort([1, 4, 2, 9, 5, 7, 3, 8, 10, 6])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); - }); + }); \ No newline at end of file From 1897c53b09a63cd62004a650575cc445f8d5521a Mon Sep 17 00:00:00 2001 From: benben Date: Sat, 29 Apr 2023 01:40:25 +0800 Subject: [PATCH 6/6] fix: convert naming of quick_sort.ts --- sorts/quick_sort.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorts/quick_sort.ts b/sorts/quick_sort.ts index 70cca0d5..c0fd192e 100644 --- a/sorts/quick_sort.ts +++ b/sorts/quick_sort.ts @@ -2,7 +2,7 @@ * @function quickSort * @description is an algorithm based on divide and conquer approach in which an array is split into sub-arrays and these sub arrays are recursively sorted to get final array * @see [Quick Sort](https://www.javatpoint.com/quick-sort) - * @example quickSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] + * @example QuickSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8] */ export const partition = ( @@ -34,7 +34,7 @@ export const partition = ( }; /** - * quicksort implementation + * Quicksort implementation * * @param {number[]} array * @param {number} [left=0] @@ -51,7 +51,7 @@ export const partition = ( * When the above two cases are not met */ -export const quickSort = ( +export const QuickSort = ( array: number[], left: number = 0, right: number = array.length - 1 @@ -62,11 +62,11 @@ export const quickSort = ( index = partition(array, left, right); if (left < index - 1) { - quickSort(array, left, index - 1); + QuickSort(array, left, index - 1); } if (index < right) { - quickSort(array, index, right); + QuickSort(array, index, right); } }