Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: help in sort

by uva (Sexton)
on Feb 08, 2006 at 13:53 UTC ( [id://528797]=note: print w/replies, xml ) Need Help??


in reply to Re: help in sort
in thread help in sort

thanks for your reply corion... 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

Replies are listed 'Best First'.
Re: help in sort
by jonadab (Parson) on Feb 08, 2006 at 14:02 UTC
    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.
      thank you jonadab..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://528797]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2025-01-13 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (28 votes). Check out past polls.