in the mentioned suroutine i used print statement,,it is printing values like {4,7 4,2 3,4 2,4 7,4 7,4 2,3}....in what logic it is printing these values

Oh. Unless your goal is to experiment with Perl's sort builtin, you should not print anything (or do anything with side-effects, other than _perhaps_ cache computed values). Your sorting subroutine is actually a _comparison_ subroutine, i.e., sort will call it each time it needs to compare two of the items in the list to see which should sort earlier. Each item might be compared multiple times to different other items, but it will _not_ necessarily be compared to _every_ other item (since that would be terribly inefficient). If you want to print out the list of sorted values, you should sort it first, then iterate over the list and print the values.

If you were just curious and experimenting, then like I said I think it might be a heap sort, but I could be wrong about that. The easiest way to find out would be to look in the source code for perl. No, wait, perldoc might say... Ah, yes, it does:

Perl 5.6 and earlier used a quicksort algorithm to implement sort. That algorithm was not stable, and could go quadratic. (A stable sort preserves the input order of elements that com- pare equal. Although quicksort's run time is O(NlogN) when averaged over all arrays of length N, the time can be O(N**2), quadratic behavior, for some inputs.) In 5.7, the quicksort implementation was replaced with a stable mergesort algorithm whose worst case behavior is O(NlogN). But benchmarks indi- cated that for some inputs, on some platforms, the original quicksort was faster. 5.8 has a sort pragma for limited con- trol of the sort. Its rather blunt control of the underlying algorithm may not persist into future perls, but the ability to characterize the input or output in implementation independent ways quite probably will.

So if you have a recent perl it's probably a mergesort. HTH.HAND


Sanity? Oh, yeah, I've got all kinds of sanity. Why, I've got so much sanity it's driving me crazy. In fact, I've developed whole new kinds of sanity.

In reply to Re: help in sort by jonadab
in thread help in sort by uva

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.