Created
July 30, 2015 01:20
-
-
Save kinduff/e7d07b41e1319b81715a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def sort(values) | |
length = values.size - 2 | |
swapped = true | |
while swapped | |
swapped = false | |
0.upto(length) do |i| | |
if values[i] > values[i+1] | |
values[i], values[i+1] = values[i+1], values[i] | |
swapped = true | |
end | |
end | |
end | |
return values | |
end | |
sort([7, 4, 5, 2, 9, 1]) | |
# => | |
# 7, 4, 5, 2, 9, 1 | |
# 4, 5, 2, 7, 1, 9 | |
# 4, 2, 5, 1, 7, 9 | |
# 2, 4, 1, 5, 7, 9 | |
# 2, 1, 4, 5, 7, 9 | |
# 1, 2, 4, 5, 7, 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment