Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Sort routine runs into infinite loop

by punitpawar (Sexton)
on Jan 02, 2016 at 17:16 UTC ( [id://1151697]=perlquestion: print w/replies, xml ) Need Help??

punitpawar has asked for the wisdom of the Perl Monks concerning the following question:

Hello , I am trying to write a sort routine , but it is running into an infinite loop
Its the swap that is causing this loop to be infinite ...but I am not able to identify where
#!/usr/bin/perl use strict; my @array=(12,6,2,9,15); my ($i,$j,$tmp); for ($i=0;$i<@array;$i++){ for ($j=0;$j<@array;$j++){ if ($array[$j]>$array[$j+1]){ $tmp=$array[$j+1]; $array[$j+1]=$array[$j]; $array[$j]=$tmp; } } } print "array : @array \n";

Replies are listed 'Best First'.
Re: Sort routine runs into infinite loop
by choroba (Cardinal) on Jan 02, 2016 at 17:23 UTC
    @array returns the number of elements in the array. You need $#array, the index of the last element, instead:
    #!/usr/bin/perl use warnings; use strict; my @array = (12, 6, 2, 9, 15); for (my $i = 0; $i < $#array; $i++) { for (my $j = 0; $j < $#array; $j++) { if ($array[$j] > $array[$j+1]) { my $tmp = $array[$j+1]; $array[$j+1] = $array[$j]; $array[$j] = $tmp; warn "$i $j : @array\n"; } } } print "array : @array \n";

    Inserting the debugging warn into your original code, you can see the array's getting longer and longer, as $array[$j+1] fetches an undef from the yet non-existent element after the end of the array (warnings omitted):

    0 0 : 6 12 2 9 15 0 1 : 6 2 12 9 15 0 2 : 6 2 9 12 15 0 4 : 6 2 9 12 15 0 5 : 6 2 9 12 15 0 6 : 6 2 9 12 15 0 7 : 6 2 9 12 15 0 8 : 6 2 9 12 15 0 9 : 6 2 9 12 15 0 10 : 6 2 9 12 15 0 11 : 6 2 9 12 15 0 12 : 6 2 9 12 15 0 13 : 6 2 9 12 15 0 14 : 6 2 9 12 15 0 15 : 6 2 9 12 15 0 16 : 6 2 9 12 15

    Update #2: BTW, do you know that in Perl, you can swap two variables without an explicit temp?

    ($x, $y) = ($y, $x); @array[$j, $j+1] = @array[$j+1, $j];

    Update: added explanation.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Sort routine runs into infinite loop
by pme (Monsignor) on Jan 02, 2016 at 17:35 UTC
    Hi punitpawar,

    I suppose you want to implement bubble sort (http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort#Perl).

    This is a working version of your code.

    #!/usr/bin/perl use strict; my @array=(12,6,2,9,15); my ($i,$j,$tmp); for ($i=0; $i<@array; $i++){ for ($j=$i+1;$j<@array;$j++){ if ($array[$j]<$array[$i]){ # '>' reverses the order $tmp = $array[$j]; $array[$j] = $array[$i]; $array[$i] = $tmp; } } } print "array : @array \n";

      use strict; helps a whole lot less if you:

      my ($i,$j,$tmp);

      and actually we are using Perl so you can:

      #!/usr/bin/perl use strict; use warnings; my @array = (12, 6, 2, 9, 15); for my $i (0 .. $#array) { for my $j ($i + 1 .. $#array) { @array[$j, $i] = @array[$i, $j] if $array[$j] < $array[$i]; } } print "array : @array \n";

      If you'd used warnings you'd have caught the error in the process of fixing the warnings.

      Premature optimization is the root of all job security
      So much code...

      my @array=(12,6,2,9,15); foreach (sort { $a <=> $b } @array) { print $_, " "; }

        Most assuredly, your code does not implement bubble sort.

        And if we are going to golf it, how about this:

        perl -E 'say"2 6 9 12 15"'

        So much code...
        $ perl -e 'print "$_ " for sort {$a <=> $b} qw {2 6 9 12 15}' 2 6 9 12 15

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1151697]
Approved by choroba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found