Skip to content

Instantly share code, notes, and snippets.

@pdkproitf
Last active July 13, 2019 11:49
Show Gist options
  • Save pdkproitf/7dd370aa7a54b6af769d3af0608a93c0 to your computer and use it in GitHub Desktop.
Save pdkproitf/7dd370aa7a54b6af769d3af0608a93c0 to your computer and use it in GitHub Desktop.
Implement Insertion Sort in Ruby
# Algorithms: https://www.geeksforgeeks.org/insertion-sort/
def insertion_sort(array)
(array.length).times do |j|
while j > 0
break if array[j - 1] < array[j]
array[j], array[j - 1] = array[j - 1], array[j]
j -= 1
end
end
array
end
puts '------------------------------INSERTION SORT O(n^2)--------------------------'
print insertion_sort([3,7,8,4,10,16,2,1,12])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment