Skip to content

Instantly share code, notes, and snippets.

@shuboc
Created March 18, 2019 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuboc/19795e88ba38e52f790a9c3969561c70 to your computer and use it in GitHub Desktop.
Save shuboc/19795e88ba38e52f790a9c3969561c70 to your computer and use it in GitHub Desktop.
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
const less = [];
const greater = [];
const pivot = arr[arr.length - 1];
for (let i = 0; i < arr.length - 1; ++i) {
const num = arr[i];
if (num < pivot) {
less.push(num);
} else {
greater.push(num);
}
}
return [...quickSort(less), pivot, ...quickSort(greater)];
}
const arr = [9, 4, 1, 6, 7, 3, 8, 2, 5];
quickSort(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment