in reply to unsorted list
I'd -- myself, but it doesn't let me :)
-------------------------------
The problem with that shuffle is it requires moving chunks of the array, quite inefficient with large arrays. A better linear shuffle is as follows:
Which is equivalent to:use strict; use warnings; my ($n, $t); my @arr = (0,1,2,3,4,5,6,7,8,9); for (0..($#arr-1)) { $n = int (rand() * ($#arr - $_ + 1)) + $_; $t = $arr[$n]; $arr[$n] = $arr[$_]; $arr[$_] = $t; }
I have an expanded version (not included here) which tests this with a large number of iterations and then counts how many of each number ends up in each slot, for purposes of analyzing the randomness of the sort. The results were within a percent or two of perfectly random.For all items except last Pick random item between current item and last Swap that item with current item
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: unsorted list
by eibwen (Friar) on Apr 24, 2005 at 08:35 UTC |