Skip to content

Instantly share code, notes, and snippets.

@jacek213
Created February 3, 2014 20:14
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 jacek213/8791466 to your computer and use it in GitHub Desktop.
Save jacek213/8791466 to your computer and use it in GitHub Desktop.
class Array
def quicksort(min=0, max=length-1)
if min < max
mid = partition(min, max)
quicksort(min, mid - 1)
quicksort(mid + 1, max)
end
self
end
def partition(min, max)
pivot = self[max]
border = min - 1
(min..max-1).each do |j|
if self[j] <= pivot
border += 1
self[border], self[j] = self[j], self[border]
end
end
self[border+1], self[max] = self[max], self[border+1]
border + 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment