Sorting algorithms/Comb sort: Difference between revisions

Content added Content deleted
(Go solution)
(Cleaned D code)
Line 321: Line 321:
EXIT.</lang>
EXIT.</lang>
=={{header|D}}==
=={{header|D}}==
{{works with|D|2}}<br/>
{{trans|Python}}
{{trans|Python}}
<lang d>import std.algorithm, std.stdio;
<lang d>import std.stdio, std.algorithm;


void combsort(T)(T[] input) {
void combSort(T)(T[] input) {
int gap = input.length;
int gap = input.length;
bool swaps = true;
bool swaps = true;

while (gap > 1 || swaps) {
while (gap > 1 || swaps) {
gap = max(1, cast(int)(gap / 1.2473)); // minimum gap is 1
gap = max(1, cast(int)(gap / 1.2473));
swaps = false;
swaps = false;
foreach (i; 0 .. input.length - gap)
foreach (i; 0 .. input.length - gap)
Line 340: Line 340:


void main() {
void main() {
auto a = [88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70];
auto array = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
combsort(a);
combSort(array);
assert(a == a.dup.sort);
writeln(array);
writeln(a);
}</lang>
}</lang>
Output:
<pre>[2, 4, 11, 17, 19, 24, 25, 28, 44, 46]</pre>


=={{header|Forth}}==
=={{header|Forth}}==