laziness, impatience, and hubris PerlMonks

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

by Eugene (Scribe)
 on Jun 15, 2000 at 09:22 UTC ( #18249=note: print w/replies, xml ) Need Help??

Does that mean that pre-sorted lists are sorted slowly, or is it being randomized before the actual sort?
• Comment on RE: RE: RE: Shift, Pop, Unshift and Push with Impunity!

Replies are listed 'Best First'.
RE: RE: RE: RE: Shift, Pop, Unshift and Push with Impunity!
by lhoward (Vicar) on Jun 15, 2000 at 15:13 UTC
It depends on how qsort is implemented by the C stdlib library with which perl was compiled. My guess is no, but it really depends on how your C stdlib was implemented. It is easy to add a "quicksort worst-case avoider" by not using a "use the first element as the pivot" and instead doing something like:
• adding a "sorted list detector"
• Picking the pivot randomly (instead of as the first element)
• Shuffling the list before sorting
• Using another pivot picking technique
Since not everyone knows the internals of quicksort, there is a worst case performance of O(n^2) with quicksort if the worst pivot is picked for each iteration (if you don't know what a pivot is don;t worry.... if you want to know I can explain it. 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. However, there are techniques for easily avoiding this pitfall.
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\$/

Create A New User
Node Status?
node history
Node Type: note [id://18249]
help
Chatterbox?
 [ambrus]: [Corion]: ambrus: Oh - that one would be much harder to automate... The SYNOPSIS section should mostly be a runnable program IMO, but I write only small snippets in my documentation for single functions/methods, and creating the appropriate environment for ... [Corion]: ... those in an automated fashion seems somewhat hard to me. Although it should do wonders for the test coverage ;) [haukex]: Corion: I once wrote an automated thingy for that here [haukex]: here's the code that uses it [Corion]: haukex: Hmm - maybe I can reuse that. I see that it uses Pod::Parser, which I think was one of the more fragile parsers. But if I'm statically (re)generating the tests instead of doing that "live" on the client/tester machines, that's a much smaller... [Corion]: ... problem space than trying to cater to all versions of Pod::Parser(s)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2017-02-27 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Before electricity was invented, what was the Electric Eel called?

Results (385 votes). Check out past polls.