Skip to content

Commit 27594b9

Browse files
implemented merge-sort in js with test cases (#105)
2 parents 48aaef0 + 501bf55 commit 27594b9

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ The solutions are located under `/LeetcodeProblems`. Each problem has a test fil
102102
| Algoritmhs |
103103
| - |
104104
| [Heap Sort](/SortingAlgorithms/heapSort.js) |
105+
| [Merge Sort](/SortingAlgorithms/mergeSort.js) |
105106
| [Quick Sort](/SortingAlgorithms/QuickSort.js) |
106107

107108
### Databases

SortingAlgorithms/mergeSort.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function merge(leftArr, rightArr) {
2+
let leftInd = 0, rightInd = 0;
3+
const sortedArr = [];
4+
5+
while(leftInd < leftArr.length && rightInd < rightArr.length) {
6+
if (leftArr[leftInd] < rightArr[rightInd]) {
7+
sortedArr.push(leftArr[leftInd]);
8+
leftInd++;
9+
} else {
10+
sortedArr.push(rightArr[rightInd]);
11+
rightInd++;
12+
}
13+
}
14+
15+
// join leftover values from left array or right array
16+
return sortedArr.concat(leftArr.slice(leftInd)).concat(rightArr.slice(rightInd));
17+
}
18+
19+
const mergeSort = (arr) => {
20+
if (arr.length <= 1) {
21+
return arr;
22+
}
23+
24+
const mid = Math.floor(arr.length / 2);
25+
const leftArr = mergeSort(arr.slice(0, mid));
26+
const rightArr = mergeSort(arr.slice(mid));
27+
28+
return merge(leftArr, rightArr);
29+
};
30+
31+
console.log(mergeSort([4,12,5,3,78,12,133,32, 1000, 4000]));
32+
console.log(mergeSort([14, 1, 10, 2, 3, 5, 6, 4, 7, 11, 12, 13]));
33+
console.log(mergeSort([]));
34+
console.log(mergeSort([1]));
35+
console.log(mergeSort([2, 1]));
36+
console.log(mergeSort([1,7,2,3,4,1,10,2,3,4,5]));
37+
console.log(mergeSort(["One Piece", "One-Punch Man", "My Hero Academia", "Jujutsu Kaisen", "Death Note", "Fullmetal Alchemist: Brotherhood", "Akame ga Kill", "Bleach", "Black Clover"]));
38+
39+
module.exports.mergeSort = mergeSort;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const assert = require("assert");
2+
const mergeSort = require("../SortingAlgorithms/mergeSort").mergeSort;
3+
4+
var test = function () {
5+
assert.deepEqual(
6+
[ 3, 4, 5, 12, 12, 32, 78, 133, 1000, 4000 ],
7+
mergeSort([4, 12, 5, 3, 78, 12, 133, 32, 1000, 4000])
8+
);
9+
10+
assert.deepEqual(
11+
[1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14],
12+
mergeSort([14, 1, 10, 2, 3, 5, 6, 4, 7, 11, 12, 13])
13+
);
14+
15+
assert.deepEqual([], mergeSort([]));
16+
assert.deepEqual([1], mergeSort([1]));
17+
assert.deepEqual([1, 2], mergeSort([2, 1]));
18+
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])
19+
);
20+
21+
assert.deepEqual(
22+
["Akame ga Kill", "Black Clover", "Bleach", "Death Note", "Fullmetal Alchemist: Brotherhood", "Jujutsu Kaisen", "My Hero Academia", "One Piece", "One-Punch Man"],
23+
mergeSort(["One Piece", "One-Punch Man", "My Hero Academia", "Jujutsu Kaisen", "Death Note", "Fullmetal Alchemist: Brotherhood", "Akame ga Kill", "Bleach", "Black Clover"])
24+
);
25+
};
26+
27+
module.exports.test = test;

Test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ const fs = require("fs");
55
const PROBLEMS_FOLDERS = [
66
"./LeetcodeProblems/Algorithms/easy/",
77
"./LeetcodeProblems/Algorithms/medium/",
8-
"./LeetcodeProblems/Algorithms/hard/"
8+
"./LeetcodeProblems/Algorithms/hard/",
9+
"./SortingAlgorithms/"
910
];
1011

1112
const TESTS_FOLDERS = [
1213
"./LeetcodeProblemsTests/Algorithms/easy/",
1314
"./LeetcodeProblemsTests/Algorithms/medium/",
14-
"./LeetcodeProblemsTests/Algorithms/hard/"
15+
"./LeetcodeProblemsTests/Algorithms/hard/",
16+
"./SortingAlgorithmsTest/"
1517
];
1618

1719
const REGEX_PATTERN_HIDDEN_FILES = /(^|\/)\.[^\/\.]/g;

0 commit comments

Comments
 (0)