Skip to content

Instantly share code, notes, and snippets.

@monzou
Created October 9, 2012 16:06
Show Gist options
  • Save monzou/3859765 to your computer and use it in GitHub Desktop.
Save monzou/3859765 to your computer and use it in GitHub Desktop.
def qsort(seq)
return seq if seq.empty?
p = pivot(seq)
qsort(left(seq, p)) + [p] + qsort(right(seq, p))
end
def pivot(seq)
seq.sample
end
def left(seq, p)
seq.select { |e| e < p }
end
def right(seq, p)
seq.select { |e| e > p }
end
arr = [ 1, 53, 36, 77, 22, 100, 4, 20 ]
p qsort(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment