Anonymous user
Sorting algorithms/Bogosort: Difference between revisions
→{{header|OCaml}}: re-wrote
(m) |
(→{{header|OCaml}}: re-wrote) |
||
Line 211:
<ocaml>
| e1 :: e2 :: r -> comp e1 e2 && is_sorted comp (e2 :: r)
| _
in▼
(* Fisher-Yates shuffle on lists; uses temp array *)
let shuffle l =
let ar = Array.of_list l in
let k = Random.int (n+1) in
let temp = ar.(k) in (* swap ar.(k) and ar.(n) *)
ar.(k) <- ar.(n);
# let rec bogosort li =▼
done;
else bogosort(shuffle li) ;;▼
Array.to_list ar
if is_sorted ( < ) li then
else
</ocaml>
Example:
<pre>
# bogosort [7;5;12;1;4;2;23;18] ;;
- : int list = [1; 2; 4; 5; 7; 12; 18; 23]
</
=={{header|Perl}}==
|