Anonymous user
Sorting algorithms/Insertion sort: Difference between revisions
Sorting algorithms/Insertion sort (view source)
Revision as of 21:32, 14 February 2012
, 12 years ago→{{header|Ruby}}: Added an alternative implementation.
(Updated D code) |
(→{{header|Ruby}}: Added an alternative implementation.) |
||
Line 1,490:
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>
Alternative version which doesn't swap elements but rather removes and inserts the value at the correct place:
<lang ruby>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!
|