Created
April 13, 2019 01:17
-
-
Save tomcha/543ca1b9ad88dbe0d0502a4e6bdc6835 to your computer and use it in GitHub Desktop.
クイックソート1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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