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