Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
(Added "long-hand") |
|||
Line 70: | Line 70: | ||
bubble_sort(@a); |
bubble_sort(@a); |
||
⚫ | |||
Alternate "Long Hand" Perl Method |
|||
sub Bubble_Sort { |
|||
my @list = @_; |
|||
my $temp = 0; |
|||
my $done = 0; |
|||
my $elements = $#list + 1; |
|||
while ($done == 0) { |
|||
$done = 1; |
|||
for (my $i=0;$i<$elements;$i++) { |
|||
if ($list[$i] > $list[$i+1] && ($i + 1) < $elements) { |
|||
$done = 0; |
|||
$temp = $list[$i]; |
|||
$list[$i] = $list[$i+1]; |
|||
$list[$i+1] = $temp; |
|||
} |
|||
} |
|||
} |
|||
return @list; |
|||
} |
|||
#usage |
|||
my @test = (1, 3, 256, 0, 3, 4, -1); |
|||
print join(",",Bubble_Sort(@test)); |
|||
⚫ | |||
==[[Ruby]]== |
==[[Ruby]]== |