Skip to content

Instantly share code, notes, and snippets.

@ibreathebsb
Created October 12, 2018 08:03
Show Gist options
  • Save ibreathebsb/16d9b00a8885740197decd8743f9e2ae to your computer and use it in GitHub Desktop.
Save ibreathebsb/16d9b00a8885740197decd8743f9e2ae to your computer and use it in GitHub Desktop.
function qsort(arr) {
partSort(arr, 0, arr.length - 1);
}
function partSort(arr, start, end) {
if (start >= end) {
return;
}
const p = arr[end];
let left = start;
let right = end;
while (left < right) {
while (arr[left] <= p && left < right) {
left++;
}
while (arr[right] >= p && left < right) {
right--;
}
swap(arr, left, right);
}
swap(arr, left, end);
partSort(arr, start, left - 1);
partSort(arr, left + 1, end);
}
function swap(arr, left, right) {
const tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment