Skip to content

Instantly share code, notes, and snippets.

@koleksiuk
Created January 29, 2013 17:55
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 koleksiuk/4666177 to your computer and use it in GitHub Desktop.
Save koleksiuk/4666177 to your computer and use it in GitHub Desktop.
class Array
def selection_sort
(0...length).each do |i|
min = i
(i+1...length).each do |j|
min = j if self[j] < self[i]
end
self[i], self[min] = self[min], self[i]
end
end
def insertion_sort
(1...length).each do |i|
j = i-1
while (self[i] < self[j] && j >= 0)
self[i], self[j] = self[j], self[i]
j -= 1
i -= 1
end
end
end
def quick_sort
return self if length <= 1
pivot = self[length/2]
find_all {|x| x < pivot }.quick_sort +
find_all {|x| x == pivot} +
find_all {|x| x > pivot}.quick_sort
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment