Created
September 5, 2014 09:00
-
-
Save easonhan007/3246776ce3f5c551dcdb to your computer and use it in GitHub Desktop.
how to implement insert sort using 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
# 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