use strict;
use warnings;
sub qsort(\@){
my( $array ) = @_;
_qsort( $array, 0, @$array - 1 );
}
sub _qsort{
my( $array, $left, $right ) = @_;
return $array unless $left < $right;
my $pivot_index = _qsort_partition( $array, $left, $right );
_qsort( $array, $left, $pivot_index - 1);
_qsort( $array, $pivot_index + 1, $right );
}
sub _qsort_partition {
my ($array, $left, $right) = @_;
my $pivot_value = $array->[$right];
my $store_index = $left;
for my $i ( $left .. ($right-1) ){
if( $array->[$i] <= $pivot_value ){
@$array[ $i, $store_index ] = @$array[ $store_index, $i ];
$store_index++;
}
}
@$array[ $store_index, $right ] = @$array[ $right, $store_index ];
return $store_index;
}
use JSON 'to_json';
use List::Util 'shuffle';
my @array = 0..9;
my $max_str_len = @array * 2 + 1;
my $format = "%-${max_str_len}s => %-${max_str_len}s\n";
for my $array_length ( @array ){
my @array = shuffle @array[0..$array_length];
my $before = to_json \@array;
qsort( @array );
my $after = to_json \@array;
printf $format, $before, $after;
}
пример вывода:
[0] => [0]
[1,0] => [0,1]
[2,1,0] => [0,1,2]
[2,0,1,3] => [0,1,2,3]
[2,4,3,0,1] => [0,1,2,3,4]
[2,1,3,5,0,4] => [0,1,2,3,4,5]
[4,5,2,3,0,6,1] => [0,1,2,3,4,5,6]
[1,3,0,7,6,2,5,4] => [0,1,2,3,4,5,6,7]
[5,4,3,0,8,2,1,7,6] => [0,1,2,3,4,5,6,7,8]
[6,9,8,2,7,3,5,1,0,4] => [0,1,2,3,4,5,6,7,8,9]