-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.js
49 lines (40 loc) · 1.37 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
var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];
function mergeSort(arr){
//if the array only has one element, return it
if (arr.length < 2){
return arr;
}
//get the middle value
var middle = parseInt(arr.length / 2);
//get all the values to the left of the middle
var left = arr.slice(0, middle);
//get all the values to the right of the middle
var right = arr.slice(middle, arr.length);
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right) {
//create a result array which will have the sorted values
var result = [];
//while the left side and right side have a length
while (left.length && right.length) {
//if the left side is less than or equal to the right side
if (left[0] <= right[0]) {
//push the values to the result from the left side.
result.push(left.shift());
} else {
//or else...push in the values from right side;
result.push(right.shift());
}
}
//while the left side has a length, push it in the result value;
while (left.length){
result.push(left.shift());
}
//the right side has a lengt, push it in the result value;
while (right.length){
result.push(right.shift());
}
//return the result.
return result;
}
console.log(mergeSort(a));