Sorting algorithms/Comb sort: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: Updated so minimum gap is 1, and use an example that failed the replaced function.)
Line 163: Line 163:
}
}
}</lang>
}</lang>
=={{header|C sharp|C#}}==
=={{header|D}}==
D V2, for arrays, from the Python version.
<lang csharp>using System;
<lang d>import std.algorithm, std.stdio;


void combsort(T)(T[] input) {
namespace CombSort
int gap = input.length;
{
class Program
bool swaps = true;
while (gap > 1 || swaps) {
{
gap = max(1, cast(int)(gap / 1.2473)); // minimum gap is 1
static void Main(string[] args)
{
swaps = false;
foreach (i; 0 .. input.length - gap)
int[] unsorted = new int[] { 3, 5, 1, 9, 7, 6, 8, 2, 4 };
if (input[i] > input[i + gap]) {
Console.WriteLine(string.Join(",", combSort(unsorted)));
swap(input[i], input[i + gap]);
}
swaps = true;
public static int[] combSort(int[] input)
{
double gap = input.Length;
bool swaps = true;
while (gap > 1 || swaps)
{
gap /= 1.247330950103979;
if ( gap < 1 ) { gap = 1; }
int i = 0;
swaps = false;
while (i + gap < input.Length)
{
int igap = i + (int)gap;
if (input[i] > input[igap])
{
int swap = input[i];
input[i] = input[igap];
input[igap] = swap;
swaps = true;
}
i++;
}
}
}
return input;
}
}
}
}
}

</lang>
void main() {
auto a = [88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70];
combsort(a);
assert(a == a.dup.sort);
writeln(a);
}</lang>


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