Created
April 16, 2017 18:58
-
-
Save brianxautumn/1c546475337dc3201ffde16f9eae9e40 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(input){ | |
if(input.length === 1) | |
return input; | |
var input1 = mergesort(input.slice(0, input.length/2)); | |
var input2 = mergesort(input.slice(input.length/2)); | |
return merge(input1, input2); | |
} | |
function merge(input1, input2){ | |
var merged = []; | |
while(input1.length && input2.length){ | |
if(input1[0] <= input2[0]){ | |
merged.push(input1.shift()); | |
}else{ | |
merged.push(input2.shift()); | |
} | |
} | |
while(input1.length){ | |
merged.push(input1.shift()); | |
} | |
while(input2.length){ | |
merged.push(input2.shift()); | |
} | |
return merged; | |
} | |
var test = [1 , 5, 8 ,10, 45, 100, -5]; | |
console.log(mergesort(test)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment