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

"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

Replies are listed 'Best First'.
Re^4: random #s
by choroba (Cardinal) on Oct 07, 2016 at 08:37 UTC
    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,