CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: General Discussions: General Questions:
bubble sort ??



Oct 29, 2013, 9:21 AM

Post #1 of 5 (21890 views)
bubble sort ?? Can't Post


(This post was edited by saru on Oct 30, 2013, 2:31 PM)

Chris Charley

Oct 29, 2013, 12:27 PM

Post #2 of 5 (21876 views)
Re: [aakashgh] bubble sort ?? [In reply to] Can't Post

Bubble Sort

Binary search

And, just to show what Bubble sort might look like in Perl:

use strict;
use warnings;

my @a = (12, 3, 56, 1, 44);

@a = bubble_sort(@a);

for my $int (@a) {
print $int, "\n";

sub bubble_sort {
my @array = @_;
my $swapped;

do {
$swapped = 0; # $swapped is now false
for my $i (1 .. $#array) {
if ($array[$i - 1] > $array[$i]) {
@array[$i-1, $i] = @array[$i, $i-1];
$swapped = 1; # set $swapped to true
} while ($swapped); # ... while $swapped is true
return @array;

Note that this version sorts numeric values. If that values to be sorted are strings, the line

if ($array[$i - 1] > $array[$i])

would have to be:

if ($array[$i - 1] gt $array[$i])

(This post was edited by Chris Charley on Oct 29, 2013, 1:10 PM)


Oct 30, 2013, 1:38 AM

Post #3 of 5 (21849 views)
Re: [Chris Charley] bubble sort ?? [In reply to] Can't Post

Dear Chris, Thanks for your reply. i am really happy and looking forwad to further help.But still i didn't get what my examiner wants, I am posting the my task where i have to complete it with perl command and explain it what i did on it...:(, he asked like this:

complete the function myBubbleSort, which should implement the bubble sort algorithm. my-
BubbleSort has the reference to the array, that should be sorted, as a parameter (consider the
calls to myBubbleSort in the prepared perl script). The sorting algorithm should directly be
applied to the array to be sorted (in-place = only constant amount of memory needed for sorting,
no new array allocated). If you have finished, execute the prepared script, look whether
your algorithm find the same sorting order as perlSort1 and try to find and explaination for the
result of perlSort2.

use strict;
use warnings;

sub myBubbleSort {
my $refToSortList=$_[0];

my @testList = (254324, 213, 2345, 6524, 3475, 81451, 1141);
my @perlSort = sort { $a <=> $b } @testList;

print "\n\n\n";
print "myBubbleSort: ", join(" ", @testList), "\n";
print "perlSort1: ", join(" ", @perlSort), "\n";

#####Explain the output:
print "perlSort2: ", join(" ", sort(@testList)), "\n";

print "\n\n\n";

(This post was edited by aakashgh on Oct 30, 2013, 1:44 AM)

Chris Charley

Oct 30, 2013, 9:22 AM

Post #4 of 5 (21828 views)
Re: [saru] bubble sort ?? [In reply to] Can't Post

See attached file - it uses an array reference.
Attachments: (0.71 KB)


Oct 30, 2013, 10:05 AM

Post #5 of 5 (21822 views)
Re: [Chris Charley] bubble sort ?? [In reply to] Can't Post

thanks Chris, It works but I hope this is the solution what my examiner wanted. but i don't know how to explain it. any way i will manage it.. thanks a lot ...


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives