$CBAS has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

This question is not Perl-specific but Perl is the language I love implementing things in because it gets 'em done! Hopefully it won't let me down this time.

I'm looking for a way to cycle through all unique orderings of an array. I feel like there's a trick to do this but I just can't find it!
Say I have an array [$a, $b, $c, $d, $e, $f], what would be the fastest or easiest way to loop through every possible way this array can be sorted?
The arrays I would be using are not fixed in length, so the number of possible orderings will be HUGE and hard coding the loop(s) is NOT an option :-(

Thank you for your time, I'll give it back someday :-)

CBAS

Replies are listed 'Best First'.
Re: Uniquely sorting arrays
by arturo (Vicar) on Mar 07, 2001 at 19:52 UTC

    Abstract answer: find all the permutations of the array indices, store each permutation as an arrayref, and then do this:

    # @array = (list, of, items); foreach my $perm (@index_permutation) { my @indices = @$perm; foreach (@index) { # do something with @array[$_]; } }

    For finding the permutations, take a gander at permutations? and attendant thread. You could save on storing the index permutations by encasing the inner loop in the loop that finds the permutations, I suppose.

    If you go my way and work on any sizeable array, I wouldn't want to be your sysadmin who gets a page when the memory gets critically low, though =)

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: Uniquely sorting arrays
by Corion (Patriarch) on Mar 07, 2001 at 19:54 UTC

      The arrays I would be using are not fixed in length, so the number of possible orderings will be HUGE

      Then I strongly suggest Permuting with duplicates and no memory as most of the other permutation implementations I've run into insists on generating all of the permutations and then returning them in one big list (though, Algorithm::Permute doesn't have that problem).

      I also notice you mention "unique" which makes me think that you might have cases where $a eq $c and want that accounted for in the generation of the permutations. This is another thing that my snippet handles that most permutation generators don't (and Algorithm::Permute doesn't handle that).

      Finally, you mentioned speed. I haven't run benchmarks (and would be interested in see some), but I suspect that the object overhead of Algorithm::Permute might be a disadvantage here.

      Just thought the comparisons might be helpful.

              - tye (but my friends call me "Tye")

        I'm using your snippet because it's easier on the memory, nice algorithm too (++ for that) ... but isn't it slower because it keeps going through the entire list? (I haven't looked at the code in Algorithm::Permute though)

        Well, saying 'unique' might have been confusing because what I meant was that none of the newly sorted lists should be the same. All of the elements are different anyway.
        Shows that my english needs improvement.

        Thanks everyone for the help, I really appreciate it.
        CBAS.

Re: Uniquely sorting arrays
by jeroenes (Priest) on Mar 07, 2001 at 19:56 UTC
    I take it you want to permutate your array. Why don't you take a look at Re: Pattern Generator, you might want to adopt that code to your needs.

    Hope this helps,

    Jeroen
    "We are not alone"(FZ)