Skip to content

Instantly share code, notes, and snippets.

@284km
Created July 31, 2015 22:18
Show Gist options
  • Save 284km/f4adb270c524056448f0 to your computer and use it in GitHub Desktop.
Save 284km/f4adb270c524056448f0 to your computer and use it in GitHub Desktop.
class Array
def insertion_sort
return self if size < 2
1.upto(size - 1) do |i|
# tmp = self[i]
i.downto(1) do |j|
if self[j - 1] > self[j]
self[j - 1], self[j] = self[j], self[j - 1]
p self
else
break
end
end
end
self
end
end
a = (1..10).to_a.shuffle
p a
p a.insertion_sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment