Last active
August 29, 2015 14:04
-
-
Save tkfm-yamaguchi/cf8bab2909a3c75b8673 to your computer and use it in GitHub Desktop.
easy sorting on ruby
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 my_sort! | |
1.upto(size-1) do |i| | |
i.downto(1) do |j| | |
swap!(j, j-1) if less?(j, j-1) | |
end | |
end | |
end | |
def swap!(ind1, ind2) | |
self[ind1], self[ind2] = self[ind2], self[ind1] | |
end | |
def less?(ind1, ind2) | |
self[ind1] < self[ind2] | |
end | |
end | |
a = [10, 2030, 300, 9] | |
a.my_sort! | |
p a | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment