Created
September 25, 2012 19:41
-
-
Save prettymuchbryce/3783986 to your computer and use it in GitHub Desktop.
Ruby Quicksort
This file contains 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
class QuickSort | |
def recurse(low, high) | |
i = low | |
j = high | |
pivot = @list[(low + (high-low)/2).floor] | |
while i <= j | |
while @list[i] < pivot | |
i+=1 | |
end | |
while @list[j] > pivot | |
j-=1 | |
end | |
if i <= j | |
@list[i], @list[j] = @list[j], @list[i] | |
i+=1 | |
j-=1 | |
end | |
end | |
if low < j | |
recurse(low,j) | |
end | |
if i < high | |
recurse(i,high) | |
end | |
end | |
def sort(list) | |
if list == nil || list.length <= 1 | |
return list | |
end | |
@list = list | |
recurse(0,@list.length-1) | |
@list | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment