Skip to content

Instantly share code, notes, and snippets.

@jinnyMcKindy
Created June 29, 2020 20:37
Show Gist options
  • Save jinnyMcKindy/31c0815b1e9e588ae7f85681f4688259 to your computer and use it in GitHub Desktop.
Save jinnyMcKindy/31c0815b1e9e588ae7f85681f4688259 to your computer and use it in GitHub Desktop.
mergeSort
function mergeSort(array, half = array.length/2) {
if(array.length < 2) { return array; }
const left = array.splice(0, half);
return merger(mergeSort(left), mergeSort(array));
}
function merger(left, right) {
const arr = [];
while(left.length && right.length) {
if(left[0] < right[0]) {
arr.push(left.shift());
}
else {
arr.push(right.shift());
}
}
return [...arr,...left,...right]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment