Sorting algorithms/Shell sort: Difference between revisions

m (→‎{{header|C}}: formatting)
Line 281:
 
This method sorts in place. If you want to preserve your unsorted list, copy it first.
<lang perl>use strict;
 
use strict;
use warnings;
sub shell { # (int[a])
my @iary = @_;
my $inc = int(($#iary+1) / 2);
while ($inc > 0) {
forforeach (my $i= ($inc; $i <.. $#iary+1; $i++) {
my $j = $i;
my $temp = $iary[$i];
while (($ji >= $inc) and&& ($iary[$ji-$inc] > $temp)) {
$iary[$ji] = $iary[$ji-$inc];
$ji -= $inc;
}
$iary[$ji] = $temp;
}
if ($inc == 2) {
$inc = 1;
} else {
else {
$inc *= (5.0 / 11);
$inc = int($inc);
}
}
return (@iary);
}
my @data = (22, 7, 2, -5, 8, 4);
print "input =@data\n";
@data=shell(@data);
print "output=@data\n"; # [-5, 2, 4, 7, 8, 22]</lang>
</lang>
 
=={{header|Python}}==
Anonymous user