2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,21 @@
defmodule Sort do
def comb_sort([]), do: []
def comb_sort(input) do
comb_sort(List.to_tuple(input), length(input), 0) |> Tuple.to_list
end
defp comb_sort(output, 1, 0), do: output
defp comb_sort(input, gap, _) do
gap = max(trunc(gap / 1.25), 1)
{output,swaps} = Enum.reduce(0..tuple_size(input)-gap-1, {input,0}, fn i,{acc,swap} ->
if (x = elem(acc,i)) > (y = elem(acc,i+gap)) do
{acc |> put_elem(i,y) |> put_elem(i+gap,x), 1}
else
{acc,swap}
end
end)
comb_sort(output, gap, swaps)
end
end
(for _ <- 1..20, do: :rand.uniform(20)) |> IO.inspect |> Sort.comb_sort |> IO.inspect