Download
#!/usr/bin/perl -w
use strict;
package main;
my @a = (1, 6, 5, 3, 8, 6, 9, 7, 2, 4, 0);
&CombSort(\@a);
print join("\n", @a) . "\n";
sub NewGap()
{
my ($gap) = @_;
$gap = $gap * 10 / 13;
$gap = 1 if $gap < 1;
$gap = 11 if $gap == 9 or $gap == 10;
return $gap;
}
sub CombSort()
{
my ($Arrayref) = @_;
my $gap = scalar @{$Arrayref};
my $swapped;
do
{
$gap = &NewGap($gap);
$swapped = 0;
for (my $i = 0; $i < scalar @{$Arrayref} - $gap; $i++)
{
if (${$Arrayref}[$i] > ${$Arrayref}[$i+$gap])
{
($ {$Arrayref}[$i], $ {$Arrayref}[$i+$gap]) = ($ {$Arrayref}[$i+$gap], $ {$Arrayref}[$i]);
$swapped = 1;
}
}
} until ($gap == 1 and !$swapped)
}