Skip to content

Instantly share code, notes, and snippets.

@namse
Last active August 21, 2016 08:28
Show Gist options
  • Save namse/19d1560be298291a359c4b778a06d669 to your computer and use it in GitHub Desktop.
Save namse/19d1560be298291a359c4b778a06d669 to your computer and use it in GitHub Desktop.
// from https://gist.github.com/zygygy/0feaff3f25ebe7e7e4dafa7b7f18c061
function partition(seq, pred) {
const first = [];
const second = [];
seq.forEach(i => {
if (pred(i)) {
first.push(i);
} else {
second.push(i);
}
});
return {
first,
second,
}
}
function qsort(con) {
if (!!!con || !!!con.length) {
return [];
}
const head = con[0];
const tail = con.slice(1);
const comp = (v) => v < head;
const {
first: low,
second: high,
} = partition(tail, comp);
return [].concat(
qsort(low),
[head],
qsort(high)
);
}
console.log(qsort([5,3,6,1,5,67,7,2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment