Skip to content

Instantly share code, notes, and snippets.

@lupomontero
Created March 27, 2017 15:01
Show Gist options
  • Save lupomontero/71ef970c04cecdc06cab31f804c3c969 to your computer and use it in GitHub Desktop.
Save lupomontero/71ef970c04cecdc06cab31f804c3c969 to your computer and use it in GitHub Desktop.
function quickSort(array) {
if (array.length < 1) {
return [];
}
var left = [];
var right = [];
var pivot = array[0];
for (var i = 1; i < array.length; i++) {
if (array[i] < pivot) {
left.push(array[i]);
}
else {
right.push(array[i]);
}
}
return [].concat(quickSort(left), pivot, quickSort(right));
}
console.log(quickSort([4, 9, 2, 1, 6, 3, 8])); // [1,2,3,4,6,8,9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment