Skip to content

Instantly share code, notes, and snippets.

@alexeykomov
Created August 26, 2016 14:07
Show Gist options
  • Save alexeykomov/95b9bb40990856c8e63cd56035744340 to your computer and use it in GitHub Desktop.
Save alexeykomov/95b9bb40990856c8e63cd56035744340 to your computer and use it in GitHub Desktop.
function mergeSort(aInput) {
if (aInput.length == 0 || aInput.length == 1) {
return [...aInput];
} else {
const middleIndex = Math.floor(aInput.length / 2);
return merge(mergeSort(aInput.slice(0, middleIndex)),
mergeSort(aInput.slice(middleIndex)));
}
}
function merge(aLeftArray, aRightArray) {
let counterLeft = 0;
let counterRight = 0;
let result = [];
while (counterLeft < aLeftArray.length && counterRight < aRightArray.length) {
if (aLeftArray[counterLeft] < aRightArray[counterRight]) {
result.push(aLeftArray[counterLeft]);
counterLeft++;
} else {
result.push(aRightArray[counterRight]);
counterRight++;
}
}
return result.concat(aLeftArray.slice(counterLeft)).concat(aRightArray.slice(
counterRight));
}
console.log(mergeSort([17, 2, 3, 1, 5, 2, 12, 1]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment