Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 13, 2019 01:17
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/543ca1b9ad88dbe0d0502a4e6bdc6835 to your computer and use it in GitHub Desktop.
Save tomcha/543ca1b9ad88dbe0d0502a4e6bdc6835 to your computer and use it in GitHub Desktop.
クイックソート1
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use DDP { deparse => 1 };
my @seeds;
for (1..100){
push(@seeds, int(rand(100)));
}
p @seeds;
my @result = quicksort(\@seeds);
p @result;
sub quicksort{
my $array = shift;
if ((scalar @$array) == 0){ return;
} elsif((scalar @$array) == 1){
return $array->[0];
} else {
my $center = shift(@$array);
my @right;
my @left;
for my $i (@$array){
if ($center < $i){
push (@right, $i);
} else {
push (@left, $i);
}
}
return (quicksort(\@left), $center, quicksort(\@right));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment