Sorting algorithms/Insertion sort: Difference between revisions

Content added Content deleted
(→‎OCaml: clean-up & simplify)
Line 3,727: Line 3,727:


=={{header|OCaml}}==
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec insert lst x =
<syntaxhighlight lang="ocaml">let rec insert lst x =
match lst with
match lst with
[] -> [x]
| y :: ys when x > y -> y :: insert ys x
| y :: ys when x <= y -> x :: y :: ys
| _ -> x :: lst
| y :: ys -> y :: insert ys x


let insertion_sort = List.fold_left insert []
;;
let insertion_sort = List.fold_left insert [];;


insertion_sort [6;8;5;9;3;2;1;4;7];;</syntaxhighlight>
let () = [6; 8; 5; 9; 3; 2; 1; 4; 7]
|> insertion_sort |> List.iter (Printf.printf " %u") |> print_newline</syntaxhighlight>
{{out}}
<pre> 1 2 3 4 5 6 7 8 9</pre>


=={{header|Oforth}}==
=={{header|Oforth}}==