Sorting algorithms/Selection sort: Difference between revisions

copied ML from wikibooks http://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Selection_sort#ML; added ocaml, haskell
(copied ML from wikibooks http://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Selection_sort#ML; added ocaml, haskell)
Line 256:
Sorted array : -5 -2 0 1 3 4 6 7 8 9
</pre>
 
=={{header|Haskell}}==
<lang haskell>selectionSort [] = []
selectionSort (first:lst) = selectR first [] lst where
selectR small output [] = small : selectionSort output
selectR small output (x:xs) | x < small = selectR x (small:output) xs
| otherwise = selectR small (x:output) xs</lang>
 
=={{header|Java}}==
Line 298 ⟶ 305:
print data
</pre>
 
=={{header|OCaml}}==
<lang ocaml>let rec selection_sort = function
[] -> []
| first::lst ->
let rec select_r small output = function
[] -> small :: selection_sort output
| x::xs when x < small -> select_r x (small::output) xs
| x::xs -> select_r small (x::output) xs
in
select_r first [] lst</lang>
 
=={{header|Standard ML}}==
<lang sml>fun selection_sort [] = []
| selection_sort (first::lst) =
let
fun select_r small ([], output) = small :: selection_sort output
| select_r small (x::xs, output) =
if x < small then
select_r x (xs, small::output)
else
select_r small (xs, x::output)
in
select_r first (lst, [])
end</lang>
Anonymous user