forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mergeSort.js
49 lines (46 loc) · 969 Bytes
/
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
/*
* Implementation of merge sort algorithm
* Time complexity: O(size*log(size))
* Space complexity: O(size)
*/
function merge (left, right) {
let i = 0;
let j = 0;
let size = left.length + right.length;
let arr = [];
for (let k = 0; k < size; k++) {
if (i > left.length - 1 && j < right.length) {
arr[k] = right[j];
j++;
} else if (j > right.length - 1 && i < left.length) {
arr[k] = left[i];
i++;
} else if (left[i] < right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
}
return arr;
}
/*
* :param arr: array to be sorted
*/
function mergeSort (arr) {
if (arr.length < 2) {
return arr;
}
let middle = parseInt(arr.length / 2);
let left = arr.slice(0, middle);
let right = arr.slice(middle, arr.length);
return merge(mergeSort(left), mergeSort(right));
}
function main () {
let arr = [2, 1, 6, 444, 8, 99, 10];
console.log('Sorted data is :');
arr = mergeSort(arr);
console.log(arr);
}
main();