Sorting algorithms/Comb sort: Difference between revisions

Added Elixir
No edit summary
(Added Elixir)
Line 162:
<pre>88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70
0,4,5,8,14,18,20,31,33,44,62,70,73,75,76,78,81,82,84,88</pre>
 
=={{header|AWK}}==
<lang awk>function combsort( a, len, gap, igap, swap, swaps, i )
Line 277 ⟶ 278:
}
}</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>using System;
Line 316 ⟶ 318:
}
}</lang>
 
=={{header|COBOL}}==
This excerpt contains just enough of the procedure division to show the workings. See the example for the bubble sort for a more complete program.
Line 359 ⟶ 362:
F-999.
EXIT.</lang>
 
=={{header|D}}==
{{trans|Python}}
Line 385 ⟶ 389:
{{out}}
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>
 
=={{header|Eiffel}}==
<lang Eiffel>
Line 501 ⟶ 506:
sorted:
-7 1 2 5 7 95 99
</pre>
 
=={{header|Elixir}}==
<lang elixir>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</lang>
 
{{out}}
<pre>
[10, 7, 8, 13, 4, 11, 13, 12, 18, 11, 5, 7, 3, 4, 15, 1, 17, 16, 7, 14]
[1, 3, 4, 4, 5, 7, 7, 7, 8, 10, 11, 11, 12, 13, 13, 14, 15, 16, 17, 18]
</pre>
 
Line 597 ⟶ 631:
end program Combsort_Demo</lang>
 
=={{header|FreeBASIC}}==
<lang FreeBASIC>' version 05-07-2015
Line 954 ⟶ 989:
[0, 1, 3, 4, 5, 9]
</pre>
 
=={{header|jq}}==
{{works with|jq|1.4}}
Line 1,333 ⟶ 1,369:
{CombSort Arr}
{Show {Array.toRecord unit Arr}}</lang>
 
=={{header|PARI/GP}}==
<lang parigp>combSort(v)={
Line 1,517 ⟶ 1,554:
end comb_sort;
</lang>
 
=={{header|PowerShell}}==
Massaging gap to always hit 11. Based on PowerShell from [[Cocktail Sort]]
Line 1,884 ⟶ 1,922:
Produces this output:
<pre>12 14 23 24 24 31 35 38 46 51 57 57 58 76 78 89 92 95 97 99</pre>
 
=={{header|TI-83 BASIC}}==
Requires [[Insertion sort#TI-83 BASIC|prgmSORTINS]]. Gap division of 1.3. Switches to [[Insertion sort]] when gap is less than 5.
Line 1,914 ⟶ 1,953:
 
{{omit from|GUISS}}
 
=={{header|uBasic/4tH}}==
<lang>PRINT "Comb sort:"
Anonymous user