Skip to content

Commit 069fa94

Browse files
committed
Added Perl implementation of Bubble sort
1 parent d0a2fe4 commit 069fa94

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Bubble_Sort;
2+
3+
use strict;
4+
use warnings;
5+
6+
sub bubble_sort {
7+
my $array_ref = shift;
8+
my @array = @$array_ref;
9+
my $sorted = 0;
10+
11+
while( !$sorted ) {
12+
my $modified = 0;
13+
for( my $i = 1; $i <= $#array; $i++ ) {
14+
if( $array[ $i - 1 ] > $array[ $i ] ) {
15+
$modified = 1;
16+
my $temp = $array[ $i ];
17+
$array[ $i ] = $array[ $i - 1 ];
18+
$array[ $i - 1 ] = $temp;
19+
}
20+
}
21+
$sorted = ( $modified ? 0 : 1 );
22+
}
23+
24+
return \@array;
25+
}
26+
27+
1;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use Test::More;
2+
3+
use Bubble_Sort;
4+
5+
my @array = (5, 4, 3, 2, 1);
6+
7+
is_deeply( Bubble_Sort::bubble_sort( \@array ),
8+
[1, 2, 3, 4, 5],
9+
"Got a sorted array");
10+
11+
done_testing;

0 commit comments

Comments
 (0)