Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
(→‎{{header|C++}}: same algorithm, better style)
(+Icon+Unicon)
Line 549: Line 549:
A(j+1) = value
A(j+1) = value
ENDDO</lang>
ENDDO</lang>

== Icon and Unicon ==
==={{header|Icon}}===
<lang Icon>procedure main() #: demonstrate various ways to sort a list and string
demosort(insertionsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end

procedure insertionsort(X,op) #: return sorted X
local i,temp

op := sortop(op,X) # select how and what we sort
every i := 2 to *X do {
temp := X[j := i]
while op(temp,X[1 <= (j -:= 1)]) do
X[j+1] := X[j]
X[j+1] := temp
}
return X
end</lang>

Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]].

Sample Output:<pre>Sorting Demo using procedure insertionsort
on list : [ 3 14 1 5 9 2 6 3 ]
with op = &null: [ 1 2 3 3 5 6 9 14 ] (0 ms)
with op = "numeric": [ 1 2 3 3 5 6 9 14 ] (0 ms)
with op = "string": [ 1 14 2 3 3 5 6 9 ] (0 ms)
with op = ">>": [ 9 6 5 3 3 2 14 1 ] (0 ms)
with op = ">": [ 14 9 6 5 3 3 2 1 ] (0 ms)
with op = procedure cmp: [ 1 2 3 3 5 6 9 14 ] (0 ms)
with op = "cmp": [ 1 2 3 3 5 6 9 14 ] (0 ms)
on string : "qwerty"
with op = &null: "eqrtwy" (0 ms)</pre>

==={{header|Unicon}}===
The Icon solution works in Unicon.


=={{header|J}}==
=={{header|J}}==