Created
June 29, 2020 20:37
-
-
Save jinnyMcKindy/31c0815b1e9e588ae7f85681f4688259 to your computer and use it in GitHub Desktop.
mergeSort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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