Created
January 29, 2013 17:55
-
-
Save koleksiuk/4666177 to your computer and use it in GitHub Desktop.
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 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