Skip to content

Instantly share code, notes, and snippets.

@kinduff
Created July 30, 2015 01:30
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 kinduff/cc370c83be6c1753e375 to your computer and use it in GitHub Desktop.
Save kinduff/cc370c83be6c1753e375 to your computer and use it in GitHub Desktop.
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