Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
クイックソートその2
#!/usr/bin/env perl
use strict;
use warnings;
use feature 'say';
use DDP { deparse => 1 };
my @seeds;
for (1..10){
push(@seeds, int(rand(100)));
}
p @seeds;
quick_sort(\@seeds, 0, ((scalar @seeds) - 1));
p @seeds;
sub quick_sort{
my $array = shift;
my $head = shift;
my $tail = shift;
my $l = $head;
my $r = $tail;
my $center_val = $array->[$l];
my $now_val = $array->[$r];
if ($l >= $r){
return;
} else {
while ($l != $r){
if ($now_val > $center_val){
$array->[$r] = $now_val;
$r--;
$now_val = $array->[$r];
} else {
$array->[$l] = $now_val;
$l++;
$now_val = $array->[$l];
}
}
$array->[$l] = $center_val;
quick_sort($array, $head, $l -1);
quick_sort($array, $l + 1, $tail);
}
}
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.