We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Dear Could you tell me where i should count in this code by javascript Thanks.
function mergeSort(arr) {
if (arr.length === 1)
return arr;
var middle = Math.floor(arr.length / 2);
var left = arr.slice(0, middle);
var right = arr.slice(middle, arr.length);
return merge(mergeSort(left),mergeSort(right));
}
function merge(left, right) {
var indexLeft = 0;
var indexRight = 0;
var result = [];
while (indexLeft < left.length && indexRight < right.length) {
if (left[indexLeft] < right[indexRight]) {
result.push(left[indexLeft]);
indexLeft++;
}
else {
result.push(right[indexRight])
indexRight++;
}
}
return result.concat(left.slice(indexLeft)).concat(right.slice(indexRight));
}
Merge Sort: Counting Inversions
You are viewing a single comment's thread. Return to all comments →
Dear Could you tell me where i should count in this code by javascript Thanks.