-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.js
75 lines (58 loc) · 1.78 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const fs = require('fs');
fs.readFile('inversions\\test.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const dac = data.split('\n').map(Number);
const res = mergeSort(dac);
console.log(inversions);
});
var inversions = 0;
function mergeSort(arr){
// recursivly splits the txt file and returns 0 when gets to last recursion
// that every left and right length equal to 1
if(arr.length <= 1) return arr;
// mid == file mid index
// left, right == half of file
var mid = Math.floor(arr.length/ 2);
// let mid = 1;
var left = mergeSort(arr.slice(0, mid));
var right = mergeSort(arr.slice(mid));
return sort(left, right);
function sort(left, right){
var tarr = [];
// temp array and itraters
let i = 0;
let j = 0;
while(i < left.length && j < right.length){
// while there is more values that dident itartate trough
if(left[i] <= right[j]){
tarr.push(left[i]);
i++;
} else{
// if right element smaller than left ad a inversions
if(left.length < 1){
inversions += 1;
}else{
inversions += left.length - i;
}
tarr.push(right[j]);
j++;
}
}
// push the value left
while(i < left.length){
tarr.push(left[i]);
i++;
}
while(j < right.length){
tarr.push(right[j]);
j++;
}
return tarr;
}
}
// test
// const arr = [5, 2, 7, 1, 8, 4, 6, 3, 9, 10];
// console.log(mergeSort(arr), inversions);