Last active
July 13, 2019 11:49
-
-
Save pdkproitf/7dd370aa7a54b6af769d3af0608a93c0 to your computer and use it in GitHub Desktop.
Implement Insertion Sort in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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