Pathologically Eclectic Rubbish Lister PerlMonks

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

by Eugene (Scribe)
 on Jun 13, 2000 at 21:04 UTC ( #17917=note: print w/replies, xml ) Need Help??

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

I am curious now, what is the order of a sort function and what algorithm does it use?

Replies are listed 'Best First'.
RE: RE: Shift, Pop, Unshift and Push with Impunity!
by lhoward (Vicar) on Jun 13, 2000 at 21:10 UTC
Perl's sort function sits on top of C's native qsort function which is an implementation of quicksort. O(n*log(n))!
No longer. Tom Christiansen rewrote it for perl 5.005. See Perl Delta.

EDITED

Hmmm..my memory may be wrong. I still seem to recall that Tom Christiansen wrote it, but according to the comments in the source-code it was actually written by Tom Horsley. You can find the implementation Perl uses by looking in the pp_ctl.c source-code file.

Cheers,
Ben

Does that mean that pre-sorted lists are sorted slowly, or is it being randomized before the actual sort?
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.

Create A New User
Node Status?
node history
Node Type: note [id://17917]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2018-03-23 11:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (290 votes). Check out past polls.

Notices?