Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: sort of misunderstanding of sort

by ikegami (Patriarch)
on Mar 21, 2023 at 20:57 UTC ( [id://11151107]=note: print w/replies, xml ) Need Help??


in reply to sort of misunderstanding of sort

[Woops, this doesn't answer the question.]

Checking every single pairing would be wasteful. If we know that a < b and that b < c, then we know that a < c. No need to check that one.

There are algorithms that effectively check every pairing in some circumstances, including Bubble Sort and so-called Quicksort.

Perl used to use Quicksort because it can be fast outside of the degenerate cases. But the degenerate case happens to be an already-sorted list, which is a common thing to sort.

Perl now uses Merge Sort, a much better algorithm. It always performs n log2 n comparisons for an input of size n, which is far less than the n2 approach of checking every pairing. (And it's also stable, meaning that items that compare equally will maintain their relative order. This is a very useful property.)

I linked to the Wikipedia pages of algorithms I mentioned, and they are great. They even include useful animations of the sorting algorithm in action.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-19 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found