diff --git a/README.md b/README.md index 01307b7..7e83652 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil | Algoritmhs | | - | | [Heap Sort](/SortingAlgorithms/heapSort.js) | +| [Merge Sort](/SortingAlgorithms/mergeSort.js) | | [Quick Sort](/SortingAlgorithms/QuickSort.js) | ### Databases diff --git a/SortingAlgorithms/mergeSort.js b/SortingAlgorithms/mergeSort.js new file mode 100644 index 0000000..9686427 --- /dev/null +++ b/SortingAlgorithms/mergeSort.js @@ -0,0 +1,39 @@ +function merge(leftArr, rightArr) { + let leftInd = 0, rightInd = 0; + const sortedArr = []; + + while(leftInd < leftArr.length && rightInd < rightArr.length) { + if (leftArr[leftInd] < rightArr[rightInd]) { + sortedArr.push(leftArr[leftInd]); + leftInd++; + } else { + sortedArr.push(rightArr[rightInd]); + rightInd++; + } + } + + // join leftover values from left array or right array + return sortedArr.concat(leftArr.slice(leftInd)).concat(rightArr.slice(rightInd)); +} + +const mergeSort = (arr) => { + if (arr.length <= 1) { + return arr; + } + + const mid = Math.floor(arr.length / 2); + const leftArr = mergeSort(arr.slice(0, mid)); + const rightArr = mergeSort(arr.slice(mid)); + + return merge(leftArr, rightArr); +}; + +console.log(mergeSort([4,12,5,3,78,12,133,32, 1000, 4000])); +console.log(mergeSort([14, 1, 10, 2, 3, 5, 6, 4, 7, 11, 12, 13])); +console.log(mergeSort([])); +console.log(mergeSort([1])); +console.log(mergeSort([2, 1])); +console.log(mergeSort([1,7,2,3,4,1,10,2,3,4,5])); +console.log(mergeSort(["One Piece", "One-Punch Man", "My Hero Academia", "Jujutsu Kaisen", "Death Note", "Fullmetal Alchemist: Brotherhood", "Akame ga Kill", "Bleach", "Black Clover"])); + +module.exports.mergeSort = mergeSort; diff --git a/SortingAlgorithmsTest/mergeSort_Test.js b/SortingAlgorithmsTest/mergeSort_Test.js new file mode 100644 index 0000000..c7d2ed7 --- /dev/null +++ b/SortingAlgorithmsTest/mergeSort_Test.js @@ -0,0 +1,27 @@ +const assert = require("assert"); +const mergeSort = require("../SortingAlgorithms/mergeSort").mergeSort; + +var test = function () { + assert.deepEqual( + [ 3, 4, 5, 12, 12, 32, 78, 133, 1000, 4000 ], + mergeSort([4, 12, 5, 3, 78, 12, 133, 32, 1000, 4000]) + ); + + assert.deepEqual( + [1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14], + mergeSort([14, 1, 10, 2, 3, 5, 6, 4, 7, 11, 12, 13]) + ); + + assert.deepEqual([], mergeSort([])); + assert.deepEqual([1], mergeSort([1])); + assert.deepEqual([1, 2], mergeSort([2, 1])); + assert.deepEqual([1, 1, 2, 2, 3, 3, 4, 4, 5, 7, 10], mergeSort([1,7,2,3,4,1,10,2,3,4,5]) + ); + + assert.deepEqual( + ["Akame ga Kill", "Black Clover", "Bleach", "Death Note", "Fullmetal Alchemist: Brotherhood", "Jujutsu Kaisen", "My Hero Academia", "One Piece", "One-Punch Man"], + mergeSort(["One Piece", "One-Punch Man", "My Hero Academia", "Jujutsu Kaisen", "Death Note", "Fullmetal Alchemist: Brotherhood", "Akame ga Kill", "Bleach", "Black Clover"]) + ); +}; + +module.exports.test = test; diff --git a/Test.js b/Test.js index f1c8c87..7ac738f 100644 --- a/Test.js +++ b/Test.js @@ -5,13 +5,15 @@ const fs = require("fs"); const PROBLEMS_FOLDERS = [ "./LeetcodeProblems/Algorithms/easy/", "./LeetcodeProblems/Algorithms/medium/", - "./LeetcodeProblems/Algorithms/hard/" + "./LeetcodeProblems/Algorithms/hard/", + "./SortingAlgorithms/" ]; const TESTS_FOLDERS = [ "./LeetcodeProblemsTests/Algorithms/easy/", "./LeetcodeProblemsTests/Algorithms/medium/", - "./LeetcodeProblemsTests/Algorithms/hard/" + "./LeetcodeProblemsTests/Algorithms/hard/", + "./SortingAlgorithmsTest/" ]; const REGEX_PATTERN_HIDDEN_FILES = /(^|\/)\.[^\/\.]/g;