Created
February 26, 2017 03:21
-
-
Save blackknight36/0838d14775afbb9585a75e50c7eaf30a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
def partition(ar, start, stop) | |
pivot = ar[stop] | |
pindex = start | |
for i in (start..stop-1) do | |
if ar[i] <= pivot | |
ar[i], ar[pindex] = ar[pindex], ar[i] | |
pindex += 1 | |
end | |
end | |
ar[pindex], ar[stop] = ar[stop], ar[pindex] | |
return pindex | |
end | |
def quicksort(ar, start, stop) | |
if start < stop | |
p = partition(ar, start, stop) | |
quicksort(ar, start, p-1) | |
quicksort(ar, p+1, stop) | |
end | |
return ar | |
end | |
ar = STDIN.gets.chomp.split(" ").map{|x|x.to_i}; | |
puts quicksort(ar, 0, ar.length - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment