From 83978e9d2d0dc865e18f6d0f3e23a764a49bb193 Mon Sep 17 00:00:00 2001 From: Austin Theriot Date: Sat, 28 Nov 2020 09:35:08 -0600 Subject: [PATCH 001/283] refactored merge sort to use array pointers instead of .shift() (#581) --- .../sorting/merge-sort/MergeSort.js | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/src/algorithms/sorting/merge-sort/MergeSort.js b/src/algorithms/sorting/merge-sort/MergeSort.js index 6a2f8a843c..b6eb73f02a 100644 --- a/src/algorithms/sorting/merge-sort/MergeSort.js +++ b/src/algorithms/sorting/merge-sort/MergeSort.js @@ -1,4 +1,4 @@ -import Sort from '../Sort'; +import Sort from "../Sort"; export default class MergeSort extends Sort { sort(originalArray) { @@ -24,36 +24,42 @@ export default class MergeSort extends Sort { } mergeSortedArrays(leftArray, rightArray) { - let sortedArray = []; + const sortedArray = []; - // In case if arrays are not of size 1. - while (leftArray.length && rightArray.length) { - let minimumElement = null; + // Use array pointers to exclude old elements after they have been added to the sorted array + let leftIndex = 0; + let rightIndex = 0; - // Find minimum element of two arrays. - if (this.comparator.lessThanOrEqual(leftArray[0], rightArray[0])) { - minimumElement = leftArray.shift(); - } else { - minimumElement = rightArray.shift(); - } + while (leftIndex < leftArray.length && rightIndex < rightArray.length) { + // Find the minimum element between the left and right array + if ( + this.comparator.lessThanOrEqual( + leftArray[leftIndex], + rightArray[rightIndex] + ) + ) { + sortedArray.push(leftArray[leftIndex]); - // Call visiting callback. - this.callbacks.visitingCallback(minimumElement); + // Increment index pointer to the right + leftIndex += 1; - // Push the minimum element of two arrays to the sorted array. - sortedArray.push(minimumElement); - } + // Call visiting callback. + this.callbacks.visitingCallback(leftArray[leftIndex]); + } else { + sortedArray.push(rightArray[rightIndex]); - // If one of two array still have elements we need to just concatenate - // this element to the sorted array since it is already sorted. - if (leftArray.length) { - sortedArray = sortedArray.concat(leftArray); - } + // Increment index pointer to the right + rightIndex += 1; - if (rightArray.length) { - sortedArray = sortedArray.concat(rightArray); + // Call visiting callback. + this.callbacks.visitingCallback(rightArray[rightIndex]); + } } - return sortedArray; + // There will be one element remaining from either the left OR the right + // Concatenate the remaining element into the sorted array + return sortedArray + .concat(leftArray.slice(leftIndex)) + .concat(rightArray.slice(rightIndex)); } } From 2c74ced8af09e90f60ae45ccbd656e23f3fc64f0 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Sat, 28 Nov 2020 16:43:29 +0100 Subject: [PATCH 002/283] Fix ESLint issues with MergeSort. --- .../sorting/merge-sort/MergeSort.js | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/algorithms/sorting/merge-sort/MergeSort.js b/src/algorithms/sorting/merge-sort/MergeSort.js index b6eb73f02a..23fca74e49 100644 --- a/src/algorithms/sorting/merge-sort/MergeSort.js +++ b/src/algorithms/sorting/merge-sort/MergeSort.js @@ -1,4 +1,4 @@ -import Sort from "../Sort"; +import Sort from '../Sort'; export default class MergeSort extends Sort { sort(originalArray) { @@ -26,38 +26,33 @@ export default class MergeSort extends Sort { mergeSortedArrays(leftArray, rightArray) { const sortedArray = []; - // Use array pointers to exclude old elements after they have been added to the sorted array + // Use array pointers to exclude old elements after they have been added to the sorted array. let leftIndex = 0; let rightIndex = 0; while (leftIndex < leftArray.length && rightIndex < rightArray.length) { - // Find the minimum element between the left and right array - if ( - this.comparator.lessThanOrEqual( - leftArray[leftIndex], - rightArray[rightIndex] - ) - ) { - sortedArray.push(leftArray[leftIndex]); + let minElement = null; + // Find the minimum element between the left and right array. + if (this.comparator.lessThanOrEqual(leftArray[leftIndex], rightArray[rightIndex])) { + minElement = leftArray[leftIndex]; // Increment index pointer to the right leftIndex += 1; - - // Call visiting callback. - this.callbacks.visitingCallback(leftArray[leftIndex]); } else { - sortedArray.push(rightArray[rightIndex]); - + minElement = rightArray[rightIndex]; // Increment index pointer to the right rightIndex += 1; - - // Call visiting callback. - this.callbacks.visitingCallback(rightArray[rightIndex]); } + + // Add the minimum element to the sorted array. + sortedArray.push(minElement); + + // Call visiting callback. + this.callbacks.visitingCallback(minElement); } - // There will be one element remaining from either the left OR the right - // Concatenate the remaining element into the sorted array + // There will be elements remaining from either the left OR the right + // Concatenate the remaining elements into the sorted array return sortedArray .concat(leftArray.slice(leftIndex)) .concat(rightArray.slice(rightIndex)); From 83251dfcdc1bfdb6382f15fd7e1cbbbc207a74ac Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 4 Dec 2020 18:49:31 +0100 Subject: [PATCH 003/283] Update Backers. --- BACKERS.md | 16 ++++++++++++++-- README.md | 6 ++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/BACKERS.md b/BACKERS.md index 77871fc9a4..1bf4b2cf7c 100644 --- a/BACKERS.md +++ b/BACKERS.md @@ -1,3 +1,15 @@ -# Sponsors & Backers +# Project Backers -Work on this document is in progress... +> You may support this project via ❤️️ [GitHub](https://github.com/sponsors/trekhleb) or ❤️️ [Patreon](https://www.patreon.com/trekhleb). + +## `O(2ⁿ)` Backers + +⏳ + +## `O(n²)` Backers + +⏳ + +## `O(n×log(n))` Backers + +- [bullwinkle](https://github.com/bullwinkle) diff --git a/README.md b/README.md index a438f60696..f54d8aa876 100644 --- a/README.md +++ b/README.md @@ -306,6 +306,8 @@ Below is the list of some of the most used Big O notations and their performance | **Counting sort** | n + r | n + r | n + r | n + r | Yes | r - biggest number in array | | **Radix sort** | n * k | n * k | n * k | n + k | Yes | k - length of longest key | -## Supporting the project +## Project Backers -You may support this project via ❤️️ [GitHub](https://github.com/sponsors/trekhleb) or ❤️️ [Patreon](https://www.patreon.com/trekhleb). +> You may support this project via ❤️️ [GitHub](https://github.com/sponsors/trekhleb) or ❤️️ [Patreon](https://www.patreon.com/trekhleb). + +See the [full list of backers](https://github.com/trekhleb/javascript-algorithms/blob/master/BACKERS.md) From fc1c2d877d909f1c876b4715c7f3bc99b16ee3ae Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 4 Dec 2020 18:56:25 +0100 Subject: [PATCH 004/283] Update Backers. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f54d8aa876..a17d65901f 100644 --- a/README.md +++ b/README.md @@ -310,4 +310,4 @@ Below is the list of some of the most used Big O notations and their performance > You may support this project via ❤️️ [GitHub](https://github.com/sponsors/trekhleb) or ❤️️ [Patreon](https://www.patreon.com/trekhleb). -See the [full list of backers](https://github.com/trekhleb/javascript-algorithms/blob/master/BACKERS.md) +[Folks that are backing this project](https://github.com/trekhleb/javascript-algorithms/blob/master/BACKERS.md) From 63eebef5de792bfaa1300725bf36169f4cb06f4c Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 4 Dec 2020 19:00:27 +0100 Subject: [PATCH 005/283] Update Backers. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a17d65901f..f05c93e969 100644 --- a/README.md +++ b/README.md @@ -310,4 +310,4 @@ Below is the list of some of the most used Big O notations and their performance > You may support this project via ❤️️ [GitHub](https://github.com/sponsors/trekhleb) or ❤️️ [Patreon](https://www.patreon.com/trekhleb). -[Folks that are backing this project](https://github.com/trekhleb/javascript-algorithms/blob/master/BACKERS.md) +[**1** folk is backing this project](https://github.com/trekhleb/javascript-algorithms/blob/master/BACKERS.md) From 922b3aeee2ee2863edbd934b5949308f1f80371d Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 4 Dec 2020 19:14:17 +0100 Subject: [PATCH 006/283] Update Backers. --- BACKERS.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/BACKERS.md b/BACKERS.md index 1bf4b2cf7c..1fcbd42313 100644 --- a/BACKERS.md +++ b/BACKERS.md @@ -12,4 +12,20 @@ ## `O(n×log(n))` Backers -- [bullwinkle](https://github.com/bullwinkle) + + From 7155cfe0e98ac8a394e1f3eac462c9a2dfdaa7a7 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 4 Dec 2020 19:15:59 +0100 Subject: [PATCH 007/283] Update Backers. --- BACKERS.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/BACKERS.md b/BACKERS.md index 1fcbd42313..7cad14d9b4 100644 --- a/BACKERS.md +++ b/BACKERS.md @@ -17,9 +17,6 @@ @bullwinkle From e5baba45f1be1ad4f0902d0088b7b027529222d5 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 4 Dec 2020 19:17:06 +0100 Subject: [PATCH 008/283] Update Backers. --- BACKERS.md | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/BACKERS.md b/BACKERS.md index 7cad14d9b4..f9a5bea994 100644 --- a/BACKERS.md +++ b/BACKERS.md @@ -2,14 +2,6 @@ > You may support this project via ❤️️ [GitHub](https://github.com/sponsors/trekhleb) or ❤️️ [Patreon](https://www.patreon.com/trekhleb). -## `O(2ⁿ)` Backers - -⏳ - -## `O(n²)` Backers - -⏳ - ## `O(n×log(n))` Backers From 47b4b686064b0746d2da9bb8dfe7de42430ca565 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 4 Dec 2020 19:18:34 +0100 Subject: [PATCH 010/283] Update Backers. --- BACKERS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/BACKERS.md b/BACKERS.md index ce6dfc76d9..f9a5bea994 100644 --- a/BACKERS.md +++ b/BACKERS.md @@ -12,6 +12,7 @@ width="30" height="30" /> +   bullwinkle From f1de657bca3c83692a95fe1879ae7c09ad034a28 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 4 Dec 2020 19:22:49 +0100 Subject: [PATCH 011/283] Update Backers. --- BACKERS.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/BACKERS.md b/BACKERS.md index f9a5bea994..71020a9e99 100644 --- a/BACKERS.md +++ b/BACKERS.md @@ -2,6 +2,14 @@ > You may support this project via ❤️️ [GitHub](https://github.com/sponsors/trekhleb) or ❤️️ [Patreon](https://www.patreon.com/trekhleb). +## `O(2ⁿ)` Backers + +⏳ + +## `O(n²)` Backers + +⏳ + ## `O(n×log(n))` Backers