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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
bubble sort

 



adam25bc
Novice

Dec 16, 2011, 9:47 AM

Post #1 of 9 (2417 views)
bubble sort Can't Post

Hello All,

I am trying to create a bubble sort as following :

Code
use strict; 
my @array = (3,2,5,1,4);

for (0 .. $#array -1) {
for (0 .. $#array -1) {
if ($array[$_] > $array[$_+1]) {
my $tmp = $array[$_];
$array[$_] = $array[$_+1];
$array[$_+1] = $tmp;
}
}
}
print "@array";


but I would like to have this sort shows also ascending sort as well. Also do you know how can I pars numbers to this sort , I mean instead of using 1,2,3,5 I can pass the numbers in command line.

Thanks


BillKSmith
Veteran

Dec 16, 2011, 11:22 AM

Post #2 of 9 (2402 views)
Re: [adam25bc] bubble sort [In reply to] Can't Post

A bubble sort is never better than perl's built-in sort function (Refer: perldoc -f sort).

Reverse the direction of the sort by changing your '>' to '<'.

Assign the special array @ARGV (Refer: Perldoc perlvar) to your array to get the command line variables.

The variable $tmp is needed in most languages, but not in perl. Use an array slice (Refer: perldoc perldata) to swap a pair of elements.


Code
  

(array[$_], @array[$_+1]) = (@array[$_+1], @array[$_]);



Your algorithm is correct, but it can be made faster by changing the inner loop. It is not necessary to compare elements which are already in their final place.
Good Luck,
Bill


adam25bc
Novice

Dec 16, 2011, 11:29 AM

Post #3 of 9 (2401 views)
Re: [BillKSmith] bubble sort [In reply to] Can't Post

Hi Bill,

Thank you for your message.

I will do what you said. But I have one more question
How can I pars the numbers? now I have them inside the script.

Thanks


BillKSmith
Veteran

Dec 16, 2011, 12:36 PM

Post #4 of 9 (2394 views)
Re: [adam25bc] bubble sort [In reply to] Can't Post


Quote
How can I pass the numbers? now I have them inside the script.


See @ARGV in my previous post!

I also suggest that you write the actual sort as a subroutine. You can then test it with a module such as Test::Simple, using perl's built-in sort as an oracle.
Good Luck,
Bill


adam25bc
Novice

Dec 16, 2011, 8:18 PM

Post #5 of 9 (2377 views)
Re: [BillKSmith] bubble sort [In reply to] Can't Post

Hello All,

the bubble sort script is working now, but still I can only have descending sort and I am not able to have it ascending .

following is my script


Code
 
#!/usr/bin/perl
print "Enter a series of 5 numbers and I will sort them numerically for you\n\n" ;

$count=0 ;

while ($count < 5)
{
print "Please enter a number:" ;
$input=<STDIN> ;
chomp($input) ;
@num[$count]=$input ;
$count++ ;
}

@array = @num;

for (0 .. $#array -1) {
for (0 .. $#array -1) {
if ($array[$_] > $array[$_+1]) {
my $tmp = $array[$_];
# $array[$_] = $array[$_+1];
($array[$_], $array[$_+1]) = ($array[$_+1], $array[$_]);
$array[$_+1] = $tmp;
}
}
}
print "@array";


I would appreciate any help.

Thanks


BillKSmith
Veteran

Dec 16, 2011, 8:50 PM

Post #6 of 9 (2371 views)
Re: [adam25bc] bubble sort [In reply to] Can't Post


Code
#!/usr/bin/perl 
print
"Enter a series of 5 numbers and I will sort them numerically for you\n\n";
$count = 0;
while ( $count < 5 ) {
print "Please enter a number:";
$input = <STDIN>;
chomp($input);
@num[$count] = $input;
$count++;
}
@array = @num;
for ( 0 .. $#array - 1 ) {
for ( 0 .. $#array - 1 ) {
# if ( $array[$_] > $array[ $_ + 1 ] ) { # assending
if ( $array[$_] < $array[ $_ + 1 ] ) { # decending
# my $tmp = $array[$_];
# $array[$_] = $array[$_+1];
( $array[$_], $array[ $_ + 1 ] ) = ( $array[ $_ + 1 ], $array[$_] );
# $array[ $_ + 1 ] = $tmp;
}
}
}
print "@array";


Note: $tmp is not needed at all!
The comparison operator determines which way it sorts.
Good Luck,
Bill


adam25bc
Novice

Dec 16, 2011, 9:01 PM

Post #7 of 9 (2369 views)
Re: [BillKSmith] bubble sort [In reply to] Can't Post

Hi Bill,

Thank you very much for your reply.
How can I have both ascending and descending in the same output.

for example the output should be this way

1 2 3 4 5
5 4 3 2 1

Thanks again


BillKSmith
Veteran

Dec 16, 2011, 9:05 PM

Post #8 of 9 (2365 views)
Re: [adam25bc] bubble sort [In reply to] Can't Post

Print the array backwards with the reverse function. (Refer: perldoc -f reverse)
Good Luck,
Bill


adam25bc
Novice

Dec 16, 2011, 9:33 PM

Post #9 of 9 (2361 views)
Re: [BillKSmith] bubble sort [In reply to] Can't Post

thank you very much!!
it worked!!

 
 


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

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