Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created March 16, 2019 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomcha/0357db442bad8f3d22428ec492b75553 to your computer and use it in GitHub Desktop.
Save tomcha/0357db442bad8f3d22428ec492b75553 to your computer and use it in GitHub Desktop.
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