From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,385be4c68a9e4de6 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-07-02 14:58:35 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!sn-xit-05!sn-xit-06!sn-post-01!supernews.com!corp.supernews.com!not-for-mail From: achrist@easystreet.com Newsgroups: comp.lang.ada Subject: Re: Smart sorting algorithm ? Date: Tue, 02 Jul 2002 14:57:33 -0700 Organization: Posted via Supernews, http://www.supernews.com Message-ID: <3D2221CD.3F619935@easystreet.com> X-Mailer: Mozilla 4.79 [en] (WinNT; U) X-Accept-Language: en MIME-Version: 1.0 References: <3D21D581.6EF6CB06@despammed.com> <3D21DC25.AD402F70@easystreet.com> <3D220648.6F3BE45D@despammed.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Complaints-To: newsabuse@supernews.com Xref: archiver1.google.com comp.lang.ada:26824 Date: 2002-07-02T14:57:33-07:00 List-Id: 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