Skip to content

Instantly share code, notes, and snippets.

@balinterdi
Created May 24, 2011 20:49
Show Gist options
  • Save balinterdi/989634 to your computer and use it in GitHub Desktop.
Save balinterdi/989634 to your computer and use it in GitHub Desktop.
qsort = (coll) ->
if coll.length <= 1
coll
else
less = []
more = []
idx = Math.floor(Math.random() * coll.length)
pivot = coll.splice idx, 1
for elt in coll
if elt < pivot[0] then less.push(elt) else more.push(elt)
Array.prototype.concat.call(qsort(less), pivot, qsort(more))
console.log qsort [4, 14, -3, 1, 2, 34, 7, -2, 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment