in reply to Re: random #s
in thread random #s
What it does :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; }
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 | |
by choroba (Cardinal) on Oct 07, 2016 at 08:37 UTC | |
|
Re^3: random #s
by davido (Cardinal) on Oct 07, 2016 at 15:58 UTC | |
by hippo (Archbishop) on Oct 07, 2016 at 16:31 UTC | |
by Ratazong (Monsignor) on Oct 07, 2016 at 19:36 UTC | |
|
Re^3: random #s
by GrandFather (Saint) on Oct 07, 2016 at 04:07 UTC |