in reply to random #s

That code doesn't do what you think it does. Your array has only one element, a string containing twenty numbers. If you print $RandNums instead of @array you will see that you get the same result. A Perlish way to populate your array is:

#!/usr/bin/perl use warnings; use strict; my @randNums; push @randNums, rand(20) for 1 .. 20; print "@randNums\n";

See the Wikipedia Sorting_algorithm article for different ways you might handle the sorting. If we just give you the code you won't learn much and your teacher won't be happy.

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: random #s
by cboPerl (Initiate) on Oct 07, 2016 at 03:51 UTC
    I found a previous code that makes what I want, but partially:
    use strict; use warnings; my @array = qw(1 4 6 7 23 45 12 1 2); while (not isSorted(@array)) { print +(join ", ", @array) . "\r"; my ($x, $y) = (rand(scalar @array), rand(scalar @array)); ($array[$x], $array[$y]) = ($array[$y], $array[$x]); } print "\nSort complete!\n"; print join ", ", @array; sub isSorted { my $x = shift; while (my $y = shift) { return 0 unless ($x cmp $y) < 1; $x = $y; } return 1; }
    What it does :
    1, 7, 12, 2, 23, 4, 45, 6, 1 Sort complete! 1, 1, 12, 2, 23, 4, 45, 6, 7
      "cmp" does an alphabetic sort.

      Since you have numbers, use the "<=>" operator instead.

      ALso, please use <code/> tags.

      use strict; use warnings; my @array = qw(1 4 6 7 23 45 12 1 2); while (not isSorted(@array)) { print +(join ", ", @array) . "\r"; my ($x, $y) = (rand(scalar @array), rand(scalar @array)); ($array[$x], $array[$y]) = ($array[$y], $array[$x]); } print "\nSort complete!\n"; print join ", ", @array,"\n"; sub isSorted { my $x = shift; while (my $y = shift) { return 0 unless ($x <=> $y) < 1 ; $x = $y; } return 1; }
      Output:
      Sort complete! 1, 1, 2, 4, 6, 7, 12, 23, 45

              ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall

        Nitpicking:
        @array[ $x, $y ] = @array[ $y, $x ];
        ($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,

      Is this some sort of joke?

      The algorithm you settled upon is known as a Bozosort, and is one of the algorithms studied in the infamous paper, Sorting the Slow Way: An Analysis of Perversely Awful Randomized Sorting Algorithms. That's right -- the algorithm that involves swapping random elements until the list is in order is considered not just awful, but perversely awful. "Perverse: showing a deliberate and obstinate desire to behave in a way that is unreasonable or unacceptable, often in spite of the consequences." .... that Perversely Awful -- So bad that one must be intentionally selecting the algorithm for its awfulness.

      In that paper, this algorithm is shown to have an average run-time of O(n!), so in the case of your 20 numbers, on average it will take 432902008176640000 iterations to achieve a sorted solution. In the worst case it might never finish, since the solution is left to chance. In practical terms, as iterations increases toward infinity the probability of never finding a solution declines toward zero, meaning that if you are given infinite time a solution is virtually guaranteed. Who has infinite time?

      If you are looking for an algorithm that is pretty simple to reason about, search for Bubble Sort. It's not terribly efficient, but it is also not perversely awful, and tends to be one of the easier sorting algorithms to comprehend and commit to code.


      Dave

        cboPerl wants to sort some numbers without using sort. That's the very definition of perverse and there seems to be little point in offering any type of logical advice regarding such an endeavour.

      I can't easily read that because you can't be bothered to use code tags around your code. However you might look up the difference between cmp and <=>.

      Premature optimization is the root of all job security