Skip to content

Instantly share code, notes, and snippets.

@BenGoldberg1
Created December 1, 2016 04:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenGoldberg1/f957a6201b8671f6e94bb91dbb0d0cd6 to your computer and use it in GitHub Desktop.
Save BenGoldberg1/f957a6201b8671f6e94bb91dbb0d0cd6 to your computer and use it in GitHub Desktop.
sub ben_sort {
return unless @_ > 1;
while( 1 ) {
my $prior = shift;
my @sublists = ([$prior]);
my $sublist_ix = 0;
for my $item (splice @_) {
my $cmp = $item <=> $prior;
$sublist_ix += $cmp;
if( $sublist_ix < 0 ) {
$sublist_ix = 0;
unshift @sublists, [];
} elsif( $sublist_ix > $#sublists ) {
push @sublists, [];
}
push @{ $sublists[$sublist_ix] }, $item;
$prior = $item;
}
my $done = 1;
for $partial (@sublists) {
next if @$partial < 2;
for( 1 .. $#$partial ) {
next if $partial->[0] == $partial->[$_];
$done = 0;
@$partial = ben_sort(@$partial);
last;
}
}
@_ = map @$_, splice @sublists;
return @_ if $done;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment