Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Shift, Pop, Unshift and Push with Impunity!

by jonadab (Parson)
on Feb 04, 2004 at 05:56 UTC ( #326420=note: print w/ replies, xml ) Need Help??


in reply to RE: RE: RE: RE: Shift, Pop, Unshift and Push with Impunity!
in thread Shift, Pop, Unshift and Push with Impunity!

This worst-case performance can happen if the list is already in is in sorted order and the pivot is picked by choosing the first element of the list as the pivot.

In my data structures and algorithm analysis class, they told us to select one of the elements at random to use as the pivot. This adds a small amount of overhead (the amount of time needed to pick a random number each iteration) to the average-case scenerio, but it basically eliminates the worst-case scenerio, effectively transforming it into an average-case scenerio. "random number" here can be anything that can pass as random. If your system clock has good enough precision, you can just grab that. The key thing is that you won't be picking the same element every iteration -- sometimes an early element, sometimes a late one, sometimes a middle one. So it makes no real difference how the list is sorted initially.

This is of course all moot now; these days we just use Perl's built-in sort.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/


Comment on Re: Shift, Pop, Unshift and Push with Impunity!
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (15)
As of 2014-07-11 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (235 votes), past polls