Jump to content

Sorting algorithms/Insertion sort: Difference between revisions

add Ruby
(Undo revision 33269 by 192.12.88.1 (Talk) -1 is handled by using < rather than <=)
(add Ruby)
Line 370:
seq[:] = seq[:low] + [key] + seq[low:i] + seq[i + 1:]
</lang>
 
=={{header|Ruby}}==
<lang ruby>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]</lang>
 
=={{header|Scheme}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.