Skip to content

Instantly share code, notes, and snippets.

@tomcha tomcha/quicksort.pl
Created Apr 13, 2019

Embed
What would you like to do?
クイックソート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
You can’t perform that action at this time.