Quicksort vs. Shell
06-22-2014, 02:11 AM
Post: #26
 Claudio L. Senior Member Posts: 1,885 Joined: Dec 2013
RE: Quicksort vs. Shell
(06-21-2014 02:32 PM)Werner Wrote:  In a situation where the bulk of the work is in the comparisons, binary insertion would take constant time, so I don't understand why it would be so much faster for a reversed list?
Werner

Well, the way I implemented it is like this:

You are in the middle of sorting, so you have several elements already sorted, and you are evaluating element An:

A0 ... An-1 , An , ... Unsorted ...

The algorithm takes An, and it compares it with A0 (left of the interval), and if it's less, then it inserts the element at the beginning. If not, it compares with An-1, and if it's bigger, then you leave it there. Only if it's less you start doing the bisection.

So I did the left comparison first, this means on a reversed list, it simply does one comparison, and inserts the element at the beginning of the list. So it's reversing the list with N comparisons.

I could've done the An-1 comparison first, then it would've required 2N comparisons to do the reverse-list case.

That should explain why so fast.
On the other hand, a list that's already sorted would needlessly do 2N comparisons, so for the final implementation I reversed it, now it does the An-1 comparison first.

Claudio
 « Next Oldest | Next Newest »

 Messages In This Thread Quicksort vs. Shell - Claudio L. - 06-16-2014, 09:38 PM RE: Quicksort vs. Shell - Paul Dale - 06-16-2014, 10:18 PM RE: Quicksort vs. Shell - Brad Barton - 06-17-2014, 02:51 AM RE: Quicksort vs. Shell - walter b - 06-17-2014, 06:24 AM RE: Quicksort vs. Shell - BruceH - 07-11-2022, 10:15 PM RE: Quicksort vs. Shell - Katie Wasserman - 06-17-2014, 04:25 AM RE: Quicksort vs. Shell - Paul Dale - 06-17-2014, 05:22 AM RE: Quicksort vs. Shell - Claudio L. - 06-17-2014, 03:03 PM RE: Quicksort vs. Shell - Marcus von Cube - 06-17-2014, 05:12 PM RE: Quicksort vs. Shell - Claudio L. - 06-17-2014, 06:07 PM RE: Quicksort vs. Shell - Werner - 06-17-2014, 06:51 PM RE: Quicksort vs. Shell - Claudio L. - 06-17-2014, 08:32 PM RE: Quicksort vs. Shell - Paul Dale - 06-17-2014, 09:50 PM RE: Quicksort vs. Shell - Werner - 06-17-2014, 06:53 PM RE: Quicksort vs. Shell - Claudio L. - 06-17-2014, 09:15 PM RE: Quicksort vs. Shell - Werner - 06-18-2014, 05:10 AM RE: Quicksort vs. Shell - Paul Dale - 06-17-2014, 09:45 PM RE: Quicksort vs. Shell - Paul Dale - 06-17-2014, 10:02 PM RE: Quicksort vs. Shell - Werner - 06-17-2014, 09:22 AM RE: Quicksort vs. Shell - David Hayden - 06-18-2014, 12:27 PM RE: Quicksort vs. Shell - Claudio L. - 06-18-2014, 04:29 PM RE: Quicksort vs. Shell - Werner - 06-18-2014, 06:28 PM RE: Quicksort vs. Shell - Claudio L. - 06-18-2014, 06:57 PM RE: Quicksort vs. Shell - Namir - 06-19-2014, 12:44 AM RE: Quicksort vs. Shell - Claudio L. - 06-20-2014, 02:12 PM RE: Quicksort vs. Shell - Albert Chan - 07-10-2022, 06:20 PM RE: Quicksort vs. Shell - Werner - 06-21-2014, 02:32 PM RE: Quicksort vs. Shell - Claudio L. - 06-22-2014 02:11 AM

User(s) browsing this thread: 1 Guest(s)