Yes, you are correct. How embaracing. The timing remains about the same though.
Union_1 96235
Union_2 96235
            (warning: too few iterations for a reliable count)
         s/iter   sorted unsorted
sorted     4.39       --     -66%
unsorted   1.48     196%       --
Union_1 96235
Union_2 96235
Union a b c d g e f
Union a b c d e f g
           Rate unsorted   sorted
unsorted  202/s       --     -92%
sorted   2511/s    1144%       --
Union a b c d g e f
Union a b c d e f g
sub union_1 { my $count = shift; $count--; my %saw; grep(0 == $saw{$_}++, @_); } sub union_2 { my $self = [@_]; my ($lowest_arr, $lowest, $next); my @ret = (); my $elements = scalar @_; while (1) { my $count = 0; my $work = 0; my $lowest; foreach my $a (@$self) { next unless (@$a); if (!defined($lowest) or ($a->[0] le $lowest)) { $lowest = $a->[0]; } } foreach my $a (@$self) { next unless (@$a); if ($a->[0] eq $lowest) { shift @$a; $work++; } } return @ret unless $lowest; push(@ret, $lowest); } }
A picture is worth a thousand words, but takes 200K.

In reply to Re^3: better union of sets algorithm? by gam3
in thread better union of sets algorithm? by perrin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.