Sorting algorithms/Insertion sort: Difference between revisions
Content added Content deleted
(added Fortran) |
(added scheme) |
||
Line 147: | Line 147: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>let rec insert x = function |
|||
[] -> [x] |
|||
| y :: ys -> |
|||
if x <= y then x :: y :: ys |
|||
else y :: insert x ys |
|||
;; |
|||
let insertion_sort lst = List.fold_right insert lst [];; |
|||
insertion_sort [6;8;5;9;3;2;1;4;7];;</ocaml> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 227: | Line 227: | ||
seq[:] = seq[:low] + [key] + seq[low:i] + seq[i + 1:] |
seq[:] = seq[:low] + [key] + seq[low:i] + seq[i + 1:] |
||
</pre> |
</pre> |
||
=={{header|Scheme}}== |
|||
<scheme>(define (insert x lst) |
|||
(if (null? lst) |
|||
(list x) |
|||
(let ((y (car lst)) |
|||
(ys (cdr lst))) |
|||
(if (<= x y) |
|||
(cons x lst) |
|||
(cons y (insert x ys)))))) |
|||
(define (insertion-sort lst) |
|||
(if (null? lst) |
|||
'() |
|||
(insert (car lst) |
|||
(insertion-sort (cdr lst))))) |
|||
(insertion-sort '(6 8 5 9 3 2 1 4 7))</scheme> |
|||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |