Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created April 13, 2019 01:25
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/13379623f1f4bf2424a29cbc2f2da2d2 to your computer and use it in GitHub Desktop.
Save tomcha/13379623f1f4bf2424a29cbc2f2da2d2 to your computer and use it in GitHub Desktop.
クイックソートその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