Skip to content

Instantly share code, notes, and snippets.

@mindplace
Created February 27, 2016 00:30
Show Gist options
  • Save mindplace/92d6a2dbc74ff596b1ab to your computer and use it in GitHub Desktop.
Save mindplace/92d6a2dbc74ff596b1ab to your computer and use it in GitHub Desktop.
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