-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMergeSort.js
More file actions
33 lines (32 loc) · 1.13 KB
/
MergeSort.js
File metadata and controls
33 lines (32 loc) · 1.13 KB
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
// Merge sort for every university
function MergeSort(array){
if (array.length <= 1){
return array;
};
const middle = Math.floor(array.length / 2);
//console.log("Hi MergeSort");
return Merge(MergeSort(array.slice(0, middle)), MergeSort(array.slice(middle)));
}
function Merge(left, right){
let result = [];
let leftIndex = 0;
let rightIndex = 0;
while(leftIndex < left.length && rightIndex < right.length){
// console.log("Hi Merge");
let leftValue = Number(left[leftIndex].ResearchOutput) + left[leftIndex].SFRatio + left[leftIndex].InterStu + left[leftIndex].FCount;
let rightValue = Number(right[rightIndex].ResearchOutput) + right[rightIndex].SFRatio + right[rightIndex].InterStu + right[rightIndex].FCount;
if(leftValue > rightValue){
result.push(left[leftIndex]);
leftIndex++;
//console.log("Hi Left");
//console.log(leftValue, rightValue);
}
else{
result.push(right[rightIndex]);
rightIndex++;
//console.log("Hi right");
//console.log(leftValue, rightValue);
}
}
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}