Created
July 30, 2015 01:30
-
-
Save kinduff/cc370c83be6c1753e375 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 - 1 | |
1.upto(length) do |i| | |
temp = values[i] | |
j = i - 1 | |
while j >= 0 and values[j] > temp | |
values[j+1] = values[j] | |
j -= 1 | |
end | |
values[j+1] = temp | |
end | |
return values | |
end | |
puts sort([7, 4, 5, 2, 9, 1]) | |
# => | |
# 4, 7, 5, 2, 9, 1 | |
# 4, 5, 7, 2, 9, 1 | |
# 2, 4, 5, 7, 9, 1 | |
# 2, 4, 5, 7, 9, 1 | |
# 1, 2, 4, 5, 7, 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment