Sorting algorithms/Strand sort: Difference between revisions
Content added Content deleted
m (Small improvements in the D entry) |
(added ocaml) |
||
Line 631: | Line 631: | ||
US Washington |
US Washington |
||
</pre> |
</pre> |
||
=={{header|OCaml}}== |
|||
{{trans|Haskell}} |
|||
<lang ocaml>let rec strand_sort (cmp : 'a -> 'a -> int) : 'a list -> 'a list = function |
|||
[] -> [] |
|||
| x::xs -> |
|||
let rec extract_strand x = function |
|||
[] -> [x], [] |
|||
| x1::xs when cmp x x1 <= 0 -> |
|||
let strand, rest = extract_strand x1 xs in x::strand, rest |
|||
| x1::xs -> |
|||
let strand, rest = extract_strand x xs in strand, x1::rest |
|||
in |
|||
let strand, rest = extract_strand x xs in |
|||
List.merge cmp strand (strand_sort cmp rest)</lang> |
|||
usage |
|||
<pre> |
|||
# strand_sort compare [170; 45; 75; -90; -802; 24; 2; 66];; |
|||
- : int list = [-802; -90; 2; 24; 45; 66; 75; 170] |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |