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| |
=={{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 |
|||
⚫ | |||
{ |
|||
bool swaps = true; |
|||
⚫ | |||
{ |
|||
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 }; |
|||
⚫ | |||
Console.WriteLine(string.Join(",", combSort(unsorted))); |
|||
⚫ | |||
} |
|||
⚫ | |||
public static int[] combSort(int[] input) |
|||
{ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
{ |
|||
gap /= 1.247330950103979; |
|||
if ( gap < 1 ) { gap = 1; } |
|||
int i = 0; |
|||
swaps = false; |
|||
while (i + gap < input.Length) |
|||
{ |
|||
int igap = i + (int)gap; |
|||
⚫ | |||
{ |
|||
int swap = input[i]; |
|||
⚫ | |||
input[igap] = swap; |
|||
swaps = true; |
|||
} |
|||
i++; |
|||
} |
|||
} |
} |
||
return input; |
|||
} |
|||
} |
} |
||
} |
} |
||
⚫ | |||
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); |
|||
⚫ | |||
=={{header|Forth}}== |
=={{header|Forth}}== |