all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,17 @@
class Array
def insertionsort!
1.upto(length - 1) do |i|
value = self[i]
j = i - 1
while j >= 0 and self[j] > value
self[j+1] = self[j]
j -= 1
end
self[j+1] = value
end
self
end
end
ary = [7,6,5,9,8,4,3,1,2,0]
ary.insertionsort!
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

View file

@ -0,0 +1,17 @@
class Array
def insertionsort!
return if length < 2
1.upto(length - 1) do |i|
value = delete_at i
j = i - 1
j -= 1 while j >= 0 && value < self[j]
insert(j + 1, value)
end
self
end
end
ary = [7,6,5,9,8,4,3,1,2,0]
ary.insertionsort!
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]