comp.lang.ada
 help / color / mirror / Atom feed
From: achrist@easystreet.com
Subject: Re: Smart sorting algorithm ?
Date: Tue, 02 Jul 2002 14:57:33 -0700
Date: 2002-07-02T14:57:33-07:00	[thread overview]
Message-ID: <3D2221CD.3F619935@easystreet.com> (raw)
In-Reply-To: 3D220648.6F3BE45D@despammed.com

Wes Groleau wrote:
> 
> > > The reason I'm asking is that I have a situation
> > > where deciding the order of two items is very slow.
> > >
> > > If the program determines that A < B  and later
> > > determines that B < C and stores this information,
> > > then if and when A ? C comes up, it can determine
> > > the answer from the stored information.
> >
> > Using, e.g., qsort, isn't it true that this should not happen?  If
> > you've compared A vs B and B vs C, then B is the pivot, and A and C
> > are on opposite sides of the pivot and won't get compared.
> 
> Interesting point.  On a test of 18 items,
> one sort took 42 compares.  

I calculate log2(factorial(18)) > 52.  If an algorithm determined 
the correct order based on 42 bits of information,  it eliminated
some comparisons based on the results of others.  

> Another test
> of 18 items took 76 compares.  

A less fortunate initial ordering.  The binary insertion sort
or the sorting network should always be close to log2(factorial(n)), 
ie average case and worst case pretty much the same.


> And a test
> of 36 items to 172 compares.  I've discarded
> detailed logs, but it seems to me I saw several
> times the same two items being compared.
> 

log2(factorial(36)) = 138.1

I guess that quicksort will repeat comparisons if there are duplicate
items in the set of items being sorted.  Or if the pivots are selected
as a median of three or such to make very bad choices of pivot less
likely, then some of the comparisons used to select the pivot may be
repeated while doing the partition.  

IAC, the problem of sorting when comparisons are expensive isn't too
much different from the general problem of sorting.  If you have data 
that has particular likely arrangements before sorting, you should 
use an algorithm that takes advantage of that.   You haven't said 
anything about whether you want to optimize best-case, worst-case, or
average case, or what's average.  

Good luck with it.


Al



  reply	other threads:[~2002-07-02 21:57 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-02 16:32 Smart sorting algorithm ? Wes Groleau
2002-07-02 17:00 ` achrist
2002-07-02 20:00   ` Wes Groleau
2002-07-02 21:57     ` achrist [this message]
2002-07-02 22:22       ` Wes Groleau
2002-07-02 22:57         ` achrist
2002-07-03 14:25           ` Wes Groleau
2002-07-08 17:36       ` Ron
2002-07-02 20:48   ` Florian Weimer
2002-07-02 17:21 ` Wilhelm Spickermann
2002-07-02 20:01   ` Wes Groleau
2002-07-02 20:22     ` Tarjei T. Jensen
2002-07-06 13:40       ` Robert Dewar
2002-07-02 18:57 ` Florian Weimer
2002-07-02 20:08   ` Wes Groleau
2002-07-08 21:54 ` Wes Groleau
2002-07-09  4:35   ` Robert Dewar
2002-07-09  7:51   ` tmoran
2002-07-09 14:48   ` Ron
2002-07-10 14:38     ` Wes Groleau
2002-07-10 18:08       ` tmoran
2002-07-10 22:14         ` Wes Groleau
2002-07-09 18:59   ` Ron
2002-07-11 14:40     ` Wes Groleau
2002-07-11 20:04       ` Robert Dewar
2002-07-15 19:37         ` Wes Groleau
2002-07-15 22:08           ` achrist
  -- strict thread matches above, loose matches on Subject: below --
2002-07-02 17:50 Gautier direct_replies_not_read
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox