Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
(Updated D code)
(→‎{{header|Ruby}}: Added an alternative implementation.)
Line 1,490: Line 1,490:
end
end
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 = [7,6,5,9,8,4,3,1,2,0]
ary.insertionsort!
ary.insertionsort!