Sorting algorithms/Insertion sort: Difference between revisions

ML section, mLite subsection
(ML section, mLite subsection)
(ML section, mLite subsection)
Line 1,301:
#(2, 2, 5, 6, 9, 10, 15, 18, 22, 22, 23, 24, 26, 27, 28, 31, 33, 34, 35, 40)
</lang>
 
=={{header|ML}}==
==={{header|mLite}}===
<lang ocaml>fun insertion_sort L =
let
fun insert
(x,[]) = [x]
| (x, y :: ys) =
if x <= y then
x :: y :: ys
else
y :: insert (x, ys)
in
foldr (insert,[]) L
end;
 
println ` insertion_sort [6,8,5,9,3,2,1,4,7];
</lang>
Output
<pre>
[1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
 
==={{header|Standard ML}}===
<lang sml>fun insertion_sort cmp = let
fun insert (x, []) = [x]
| insert (x, y::ys) =
case cmp (x, y) of GREATER => y :: insert (x, ys)
| _ => x :: y :: ys
in
foldl insert []
end;
 
insertion_sort Int.compare [6,8,5,9,3,2,1,4,7];</lang>
 
=={{header|Modula-3}}==
Anonymous user