Elixir Implementation
suggest changedefmodule Selection do
def sort(list) when is_list(list) do
do_selection(list, [])
end
def do_selection([head|[]], acc) do
acc ++ [head]
end
def do_selection(list, acc) do
min = min(list)
do_selection(:lists.delete(min, list), acc ++ [min])
end
defp min([first|[second|[]]]) do
smaller(first, second)
end
defp min([first|[second|tail]]) do
min([smaller(first, second)|tail])
end
defp smaller(e1, e2) do
if e1 <= e2 do
e1
else
e2
end
end
end
Selection.sort([100,4,10,6,9,3])
|> IO.inspect
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents