Created
March 8, 2019 11:25
-
-
Save dlucidone/d81760583d756304967104b645d530ae to your computer and use it in GitHub Desktop.
QuickSort
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