var counter function mergeSortR array if array length return array var

 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
var counter = 0;
function mergeSortR(array) {
if (array.length <= 1)
return array;
var left = [], right = [];
var midd = Number.parseInt(array.length / 2);
for (var i = 0; i < midd; i++)
left.push(array[i]);
for (i = midd; i < array.length; i++)
right.push(array[i]);
left = mergeSortR(left);
right = mergeSortR(right);
var result = [];
while (left.length > 0 && right.length > 0) {
if (left[0] <= right[0]){
result.push(left.shift());
counter++;
}
else
result.push(right.shift());
}
if (left.length > 0)
result.push(left.shift());
if (right.length > 0)
result.push(right.shift());
return result;
}