-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSort.js
49 lines (37 loc) · 1.71 KB
/
mergeSort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(function (global) {
// implementation of mergeSort algorithm
function mergeSort (arr) {
// if arr passed is a single element then no sorting is needed
// return the one element array instead
if (arr.length > 1) {
var midIndex,
leftPartSorted,
rightPartSorted,
arrSorted = [],
leftPartSortedIndex = 0,
rightPartSortedIndex = 0;
// find middle index of arr passed as argument
midIndex = Math.ceil(arr.length / 2) - 1;
// divide arr in 2 halves using the middle index of `arr`
// recursively call @mergeSort on each half until leftPartSorted and rightPartSorted
// are each equal to a one-element array
leftPartSorted = mergeSort(arr.slice(0, midIndex + 1));
rightPartSorted = mergeSort(arr.slice(midIndex + 1));
// traverse each sorted half of `arr` pushing the samellest element first
for (var k = 0; k < arr.length; k++) {
if (leftPartSorted[leftPartSortedIndex] <= rightPartSorted[rightPartSortedIndex] || rightPartSortedIndex > rightPartSorted.length - 1) {
arrSorted.push(leftPartSorted[leftPartSortedIndex]);
leftPartSortedIndex++;
}
else {
arrSorted.push(rightPartSorted[rightPartSortedIndex]);
rightPartSortedIndex++;
}
}
// return the sorted version of `arr`
return arrSorted
}
return arr;
}
global.mergeSort = mergeSort;
} (window));