Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
(Temporarily removed the Batch File solution)
Line 1,582: Line 1,582:


insertion_sort [6;8;5;9;3;2;1;4;7];;</lang>
insertion_sort [6;8;5;9;3;2;1;4;7];;</lang>

=={{header|Oforth}}==

Returns a new sorted list.

<lang Oforth>: insertionSort(a)
{
| l i j v |
ListBuffer new dup addAll(a) ->l
2 l size for: i [
l at(i) ->v
i 1 - ->j
while(j) [
l at(j) dup v <= ifTrue: [ drop break ]
j 1 + l put
j 1 - ->j
]
l put(j 1 +, v)
]
l
}</lang>

{{out}}
<pre>
>[ 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 ] insertionSort .
[-31, 0, 1, 2, 2, 4, 65, 83, 99, 782] ok
>
</pre>


=={{header|Oz}}==
=={{header|Oz}}==