Skip to content

Instantly share code, notes, and snippets.

@easonhan007
Created September 5, 2014 09:00
Show Gist options
  • Save easonhan007/3246776ce3f5c551dcdb to your computer and use it in GitHub Desktop.
Save easonhan007/3246776ce3f5c551dcdb to your computer and use it in GitHub Desktop.
how to implement insert sort using ruby
# insert sort using ruby
def insert_sort(array)
(1..array.size-1).each do |index|
key = array[index]
j = index - 1
while key < array[j] && j >= 0
array[j + 1] = array[j]
j = j-1
end #while
array[j+1] = key
end #for
array
end #insert_sort
array = [3, 7, 5, 2, 1, 9, 8]
puts insert_sort(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment