Skip to content

Instantly share code, notes, and snippets.

@spasiu
Created March 8, 2017 03:55
Show Gist options
  • Select an option

  • Save spasiu/828d031817584c888921df4a41055b33 to your computer and use it in GitHub Desktop.

Select an option

Save spasiu/828d031817584c888921df4a41055b33 to your computer and use it in GitHub Desktop.
Quicksort in JavaScript
const qs = (compare, list) => {
if (list.length < 2) {
return list
}
const pivot = list[0]
const left = []
const right = []
for (const item of list.slice(1)) {
compare(pivot, item) ? left.push(item) : right.push(item)
}
return qs(compare, left).concat([pivot]).concat(qs(compare, right))
}
qs((a, b) => a > b, [0,9,3,4,1,2,5,7,6,8])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment