in reply to Re: Re: random permutations
in thread random permutations

Hi mave,

The text-book way of doing qsort 'properly' (already I'm on flame-war ground :) ), is use the median-three variant (median-three vairant means pick the first, middle and last item and choose the median of these for the partition point)with three speed enhancements: first use the two remaining items from the median-three step as sentinels (instead of having a second conditional in the innerloop), next at n=20-60ish (hardware dependent constant) switch to insertion sort, and lastly when deciding wether to swap or keep in place an equal item, choose to swap it.

Of course, all this is meaningless babble. The best thing to do is go in and test it. Hmm, unfortunately my boss got back from out of town work and is on the rampage this morning handing out more work for us all :) . I suggest a randomness test on output (chi-squared) and/or running sample data and counting the number of swaps. I also suggest using this when testing:
my @list = map $_->[1], sort {$a->[0]<=>$b->[0]} map [rand,$_], (0..99);

Which should be much faster on larger sets, and you can change the rand part to int rand $n, which can have $n be the (inverse of the) chance that two items will be equal.

Good Luck,
Gryn

Replies are listed 'Best First'.
(tye)Re: random permutations
by tye (Sage) on Mar 12, 2001 at 21:18 UTC

    Last time I recall this being tested here (I leave the search to you), it was obvious from fairly casual inspection that the list was not well randomized at all.

            - tye (but my friends call me "Tye")
      It can only be randomized as well as the random generator :) so of course, chi it against @list = map rand, (0..99).

      Ciao,
      Gryn