forked from shams-nahid/Algorithms-In-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.js
44 lines (35 loc) · 1.04 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
let sorting = (myArray, cb) => {
let result = mergeSort(myArray);
return cb(null, result);
};
let mergeSort= (myArray) => {
if (myArray.length < 2) return myArray;
let middleIndex = Math.floor(myArray.length/2);
let firstHalf = myArray.slice(0, middleIndex);
let secondHalf = myArray.slice(middleIndex, myArray.length);
return merge(mergeSort(firstHalf), mergeSort(secondHalf));
};
let merge = (myArray1, myArray2) => {
let result = [];
while (myArray1.length && myArray2.length) {
let minimumElement;
if (myArray1[0] < myArray2[0]) {
minimumElement = myArray1.shift();
} else {
minimumElement = myArray2.shift();
}
result.push(minimumElement);
}
if (myArray1.length) {
result = result.concat(myArray1);
} else {
result = result.concat(myArray2);
}
return result;
};
sorting([6000, 34, 203, 3, 746, 200, 984, 198, 764, 1, 9, 1], (err, sortedArray) => {
if (err) {
} else {
console.log(sortedArray);
}
});