Skip to content

Instantly share code, notes, and snippets.

@alaingoldman
Created February 21, 2019 07:21
Show Gist options
  • Save alaingoldman/ddf7ff687eec51f6bf8e39b68624ffec to your computer and use it in GitHub Desktop.
Save alaingoldman/ddf7ff687eec51f6bf8e39b68624ffec to your computer and use it in GitHub Desktop.
var a = ['S', 'O', 'R', 'T', 'E', 'X', 'A', 'M', 'P', 'L', 'E'];
function qSort(from, to) {
if (from >= to) {
return;
}
var pivot = a[to];
var counter = from;
for (var i = from; i < to; i++) {
if (a[i] < pivot) {
qSwap(i, counter);
counter++;
}
}
qSwap(counter, to);
qSort(from, counter - 1);
qSort(counter + 1, to);
}
function qSwap(x, y) {
let temp = a[x];
a[x] = a[y];
a[y] = temp;
}
qSort(0, a.length - 1);
console.log(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment