挿入ソート
sub insert_sort {
my @nums = @_;
for (my $x = 1; $x < @nums; $x++) {
for ( my $i = $x ; $i >= 1 && $nums[ $i - 1 ] > $nums[$i] ; $i-- ) {
my $temp = $nums[ $i - 1 ];
$nums[ $i - 1 ] = $nums[$i];
$nums[$i] = $temp;
}
}
}
ORIG: 5,3,6,2,8,4,1,7
1: (5,3,6,2,8,4,1,7)
1: 5,3 (5,3,6,2,8,4,1,7) -> (3,5,6,2,8,4,1,7)
=====
2: (3,5,6,2,8,4,1,7)
=====
3: (3,5,6,2,8,4,1,7)
3: 6,2 (3,5,6,2,8,4,1,7) -> (3,5,2,6,8,4,1,7)
2: 5,2 (3,5,2,6,8,4,1,7) -> (3,2,5,6,8,4,1,7)
1: 3,2 (3,2,5,6,8,4,1,7) -> (2,3,5,6,8,4,1,7)
=====
4: (2,3,5,6,8,4,1,7)
=====
5: (2,3,5,6,8,4,1,7)
5: 8,4 (2,3,5,6,8,4,1,7) -> (2,3,5,6,4,8,1,7)
4: 6,4 (2,3,5,6,4,8,1,7) -> (2,3,5,4,6,8,1,7)
3: 5,4 (2,3,5,4,6,8,1,7) -> (2,3,4,5,6,8,1,7)
=====
6: (2,3,4,5,6,8,1,7)
6: 8,1 (2,3,4,5,6,8,1,7) -> (2,3,4,5,6,1,8,7)
5: 6,1 (2,3,4,5,6,1,8,7) -> (2,3,4,5,1,6,8,7)
4: 5,1 (2,3,4,5,1,6,8,7) -> (2,3,4,1,5,6,8,7)
3: 4,1 (2,3,4,1,5,6,8,7) -> (2,3,1,4,5,6,8,7)
2: 3,1 (2,3,1,4,5,6,8,7) -> (2,1,3,4,5,6,8,7)
1: 2,1 (2,1,3,4,5,6,8,7) -> (1,2,3,4,5,6,8,7)
=====
7: (1,2,3,4,5,6,8,7)
7: 8,7 (1,2,3,4,5,6,8,7) -> (1,2,3,4,5,6,7,8)
=====
RESULT: 1,2,3,4,5,6,7,8