-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmerge_sort.js
46 lines (42 loc) · 1.62 KB
/
merge_sort.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
/*
Merge Sort to sort elements in array in ascending order
*/
function merge(firstHalf, secondHalf) {
// This is an auxiliary function that will make the merging process
let resultArray = [],
firstHalfIndex = 0,
secondHalfIndex = 0;
while (
firstHalfIndex < firstHalf.length &&
secondHalfIndex < secondHalf.length
) {
if (firstHalf[firstHalfIndex] < secondHalf[secondHalfIndex]) {
resultArray.push(firstHalf[firstHalfIndex]);
// Incrementing the index in order to check the next value on the next iteration
firstHalfIndex++;
} else {
resultArray.push(secondHalf[secondHalfIndex]);
// Incrementing the index in order to check the next value on the next iteration
secondHalfIndex++;
}
}
//This last destructuring add the last remanscent values from first and second half on the result array
return [
...resultArray,
...firstHalf.slice(firstHalfIndex),
...secondHalf.slice(secondHalfIndex),
];
}
function mergeSortAscending(array) {
//Checks if the testing array is unitary or empty, it can't be splitted if true
if (array.length <= 1) {
return array;
}
//The pivot is used to split the array in half, it need to be 'floored' so odd sized array can be used
const pivot = Math.floor(array.length / 2);
const firstHalf = array.slice(0, pivot);
const secondHalf = array.slice(pivot);
//Calls itself recursively, best understanding of this algorithm can be found here: wikipedia.org/wiki/Merge_sort
return merge(mergeSortAscending(firstHalf), mergeSortAscending(secondHalf));
}
console.log(mergeSortAscending([3, 5, 1, 11, 4, 1, 21, 19, 17, 6]));