I have just tried the case where you have a lot of duplicates. For this, I have populated the array to be sorted this way (with $max being set at 4999):
$w[$_] = int rand(20) foreach (0..$max);
This is the result on two runs:
$ perl bench_sort.pl Benchmark: timing 80 iterations of merge, quick... merge: 4 wallclock secs ( 3.78 usr + 0.00 sys = 3.78 CPU) @ 21 +.19/s (n=80) quick: 11 wallclock secs (10.70 usr + 0.00 sys = 10.70 CPU) @ 7 +.48/s (n=80) $ perl bench_sort.pl Benchmark: timing 80 iterations of merge, quick... merge: 4 wallclock secs ( 3.76 usr + 0.00 sys = 3.76 CPU) @ 21 +.28/s (n=80) quick: 10 wallclock secs (10.65 usr + 0.00 sys = 10.65 CPU) @ 7 +.51/s (n=80)
And now, the same using the following array initialization:
$w[$_] = int rand(10) foreach (0..$max);
And the results:
$ perl bench_sort.pl Benchmark: timing 80 iterations of merge, quick... merge: 4 wallclock secs ( 3.76 usr + 0.00 sys = 3.76 CPU) @ 21 +.28/s (n=80) quick: 22 wallclock secs (22.48 usr + 0.00 sys = 22.48 CPU) @ 3 +.56/s (n=80)
So, this seems to be another pathological case for quick sort, but much less significant than the two ones I previously tested.

BTW, testing this, I noticed that there was a small "off by one" mistake in the code I posted previously. To work really properly, the code needs to be changed this way:

my $max = 4999; $w[$_] = int rand(10) foreach (0..$max);
That led to an occasional "use of uninializeds value" warning, but does not make any of the benchmark conclusions invalid.


In reply to Re^6: sort behavior explanation required by Laurent_R
in thread sort behavior explanation required by ghosh123

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.