Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions SortingAlgorithms/mergeSort.js
Original file line number Diff line number Diff line change
@@ -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;
27 changes: 27 additions & 0 deletions SortingAlgorithmsTest/mergeSort_Test.js
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 4 additions & 2 deletions Test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down