Created
August 26, 2016 14:07
-
-
Save alexeykomov/95b9bb40990856c8e63cd56035744340 to your computer and use it in GitHub Desktop.
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(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