Skip to content

Instantly share code, notes, and snippets.

@prettymuchbryce
Created September 25, 2012 19:41
Show Gist options
  • Save prettymuchbryce/3783986 to your computer and use it in GitHub Desktop.
Save prettymuchbryce/3783986 to your computer and use it in GitHub Desktop.
Ruby Quicksort
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