Skip to content

Instantly share code, notes, and snippets.

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