Skip to content

Instantly share code, notes, and snippets.

@bluurn
Created January 29, 2019 14:07
Show Gist options
  • Save bluurn/79147b34a182b1b58bbdcc6e16d4ca92 to your computer and use it in GitHub Desktop.
Save bluurn/79147b34a182b1b58bbdcc6e16d4ca92 to your computer and use it in GitHub Desktop.
JS: Implement Quick Sort
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