Skip to content

Instantly share code, notes, and snippets.

@kulicuu
Last active June 5, 2018 22:03
Show Gist options
  • Select an option

  • Save kulicuu/054f3ac161c974cee53ea80b6e02cfc4 to your computer and use it in GitHub Desktop.

Select an option

Save kulicuu/054f3ac161c974cee53ea80b6e02cfc4 to your computer and use it in GitHub Desktop.
Quicksort in CoffeeScript
exports.quicksort = quicksort = ({ rayy }) ->
len = rayy.length
if (len is 1) or (len is 0) then return rayy
rand_idx = Math.floor(Math.random() * len)
[pivot] = rayy.splice rand_idx, 1
less = []
more = []
for num, idx in rayy
if num < pivot
less.push num
else
more.push num
less_sorted = quicksort { rayy: less }
more_sorted = quicksort { rayy: more }
less_sorted.concat pivot, more_sorted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment