Skip to content

Instantly share code, notes, and snippets.

@tomcha tomcha/shell_sort.pl
Created Mar 16, 2019

Embed
What would you like to do?
shell_sort
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use DDP { deparse => 1 };
my $time = time;
my @seeds;
for (1..100){
push(@seeds, int(rand(100)));
}
#p @seeds;
for my $i (reverse(1..5)){
my $group_index = [];
my $size = scalar @seeds;
for my $j (0..($i - 1)){
my $k = $j;
my $index = [];
while ($k <= ($size - 1)){
push (@$index, $k);
$k += $i;
}
push(@$group_index, $index);
}
for my $group (@$group_index){
bubble_sort(\@seeds, $group);
};
}
#p @seeds;
sub bubble_sort{
my $seeds_refs = shift;
my $index_refs = shift;
my $size = scalar @$index_refs;
for my $i (reverse(0..($size - 2))){
for my $j (0..$i){
my $now_index = $index_refs->[$j];
my $next_index = $index_refs->[$j + 1];
if ($seeds_refs->[$now_index] > $seeds_refs->[$next_index]){
my $tmp = $seeds_refs->[$now_index];
$seeds_refs->[$now_index] = $seeds_refs->[$next_index];
$seeds_refs->[$next_index] = $tmp;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.