in reply to Re: random #s
in thread random #s

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

Replies are listed 'Best First'.
Re^3: random #s
by NetWallah (Canon) on Oct 07, 2016 at 04:19 UTC
    "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,
Re^3: random #s
by davido (Cardinal) on Oct 07, 2016 at 15:58 UTC

    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.

        Sorting numbers without using sort is (in my eyes) a useful task in a beginners programming course. The student learns something about arrays here - and possibly something about algorithms and about partitioning a big problem (change the order of a huge list) into small steps (exchange two elements). So in my eyes it is far from being perverse.

        However the goal of the task is not reached if the student just copies some lines found in the internet. Speaking of this, I'm tempted to advise to use David Morgan-Mar's algorithm intelligent design sort - as it is easy to implement and very fast, especially for huge amounts of data.

        So long, Rata

Re^3: random #s
by GrandFather (Saint) on Oct 07, 2016 at 04:07 UTC

    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