Created
January 29, 2019 14:07
-
-
Save bluurn/79147b34a182b1b58bbdcc6e16d4ca92 to your computer and use it in GitHub Desktop.
JS: Implement Quick Sort
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 quicksort(array) { | |
if (array.length <= 1) { | |
return array; | |
} | |
var pivot = array[0]; | |
var left = []; | |
var right = []; | |
for (var i = 1; i < array.length; i++) { | |
array[i] < pivot ? left.push(array[i]) : right.push(array[i]); | |
} | |
return quicksort(left).concat(pivot, quicksort(right)); | |
}; | |
var unsorted = [23, 45, 16, 37, 3, 99, 22]; | |
var sorted = quicksort(unsorted); | |
console.log('Sorted array', sorted); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment