Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add Miranda) |
No edit summary |
||
Line 2,416: | Line 2,416: | ||
(25 12 9 8 7 3 3 2 2 1 1) |
(25 12 9 8 7 3 3 2 2 1 1) |
||
=={{header|EMal}}== |
|||
<syntaxhighlight lang="emal"> |
|||
fun insertionSort = void by List a # sort list in place |
|||
for int i = 1; i < a.length; ++i |
|||
var v = a[i] |
|||
int j |
|||
for j = i - 1; j >= 0 and a[j] > v; --j |
|||
a[j + 1] = a[j] |
|||
end |
|||
a[j + 1] = v |
|||
end |
|||
end |
|||
List lists = List[ # a list of lists |
|||
int[4, 65, 2, -31, 0, 99, 83, 782, 1], |
|||
real[5.17, 2, 5.12], |
|||
text["this", "is", "insertion", "sort"]] |
|||
for each List list in lists |
|||
writeLine("Before: " + text!list) # list as text |
|||
insertionSort(list) |
|||
writeLine("After : " + text!list) |
|||
writeLine() |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Before: [4,65,2,-31,0,99,83,782,1] |
|||
After : [-31,0,1,2,4,65,83,99,782] |
|||
Before: [5.17,2.0,5.12] |
|||
After : [2.0,5.12,5.17] |
|||
Before: [this,is,insertion,sort] |
|||
After : [insertion,is,sort,this] |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |