Skip to content

Instantly share code, notes, and snippets.

@jesiegel1
Last active August 18, 2016 15:37
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 jesiegel1/d84bf821e07f9feed9c2fd4afb6341c3 to your computer and use it in GitHub Desktop.
Save jesiegel1/d84bf821e07f9feed9c2fd4afb6341c3 to your computer and use it in GitHub Desktop.
def insert(array, right_index, value)
i = right_index
puts right_index
while array[i] > value
break if i < 0
array[i + 1] = array[i]
i -= 1
end
array[i + 1] = value
end
def insertion_sort(array)
(0...array.count - 1).each do |n|
insert(array, n, array[n+1])
end
array
end
array = [5, 7, 19, 2, -4]
insertion_sort(array)
# => [-4, 2, 5, 7, 19]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment