Skip to content

Instantly share code, notes, and snippets.

@yefim
Created February 27, 2013 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yefim/5046178 to your computer and use it in GitHub Desktop.
Save yefim/5046178 to your computer and use it in GitHub Desktop.
Quicksort implemented in CoffeeScript
partition = (A, p, l) ->
x = A[l]
i = p - 1
for j in [p..l-1]
if A[j] <= x
i++
[A[i], A[j]] = [A[j], A[i]]
[A[i+1], A[l]] = [A[l], A[i+1]]
return i + 1
sort = (A, p, l) ->
p = 0 if !p?
l = A.length-1 if !l?
if p < l
q = partition A, p, l
sort A, p, q-1
sort A, q+1, l
return A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment