Created
February 27, 2016 00:30
-
-
Save mindplace/92d6a2dbc74ff596b1ab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def insertion_sort(array) | |
sorted = false | |
while sorted == false | |
array.each_index do |i| | |
next if i == 0 | |
a = array[i] | |
b = array[i - 1] | |
if a < b | |
sorted = false | |
location = 0 | |
array[0..i].each_index do |j| | |
location = j if a < array[j] | |
break | |
end | |
array = array - [a] | |
array.insert(location, a) | |
break | |
end | |
sorted = true | |
end | |
end | |
p array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment