Skip to content

Instantly share code, notes, and snippets.

@kelvin8773
Created July 24, 2019 09:58
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 kelvin8773/5f91592c64015d534fb61b8e8e2b2dc6 to your computer and use it in GitHub Desktop.
Save kelvin8773/5f91592c64015d534fb61b8e8e2b2dc6 to your computer and use it in GitHub Desktop.
Insertion Sort in Ruby
def insertion_sort(array)
result = array.dup
def helper(array, index=0)
return array if array[index] > array[index-1] || index == 0
if array[index] < array[index-1]
array[index], array[index-1] = array[index-1], array[index]
end
helper(array, index-1)
end
for index in 1..result.size-1
result = helper(result, index)
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment