Date: Mon, 18 Apr 2016 18:13:15 +0200 From: Hans Petter Selasky <hps@selasky.org> To: Ed Schouten <ed@nuxi.nl> Cc: Aleksander Alekseev <afiskon@devzen.ru>, FreeBSD Current <freebsd-current@freebsd.org> Subject: Re: qsort() documentation Message-ID: <5715079B.9010408@selasky.org> In-Reply-To: <CABh_MKmE=p0Uq=p-zAD8i8AiWvTKDyeviviOE_piZVK%2BzHsbxA@mail.gmail.com> References: <5714C86A.8050204@selasky.org> <20160418151639.634d571d@fujitsu> <5714DC98.7090208@selasky.org> <CABh_MKmE=p0Uq=p-zAD8i8AiWvTKDyeviviOE_piZVK%2BzHsbxA@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
On 04/18/16 16:49, Ed Schouten wrote: > 2016-04-18 15:09 GMT+02:00 Hans Petter Selasky <hps@selasky.org>: >> On 04/18/16 14:16, Aleksander Alekseev wrote: >>> I suggest also add a short description of how it was achieved >>> (randomization?). >> >> I think the algorithm is switching to mergesort. I'll look up the paper and >> add that correctly before commit. > > As a Dutch person, I know the answer to this. > > Instead of picking a fixed pivot or choosing one at random, it uses an > approach called linear time median finding to find a pivot that is > 'approximately median'. There are a couple of algorithms for this, but > I think Bentley's qsort() uses this: > > https://en.wikipedia.org/wiki/Dutch_national_flag_problem > Hi, Ryan: Yes, there is quadratic behaviour still, but I believe the order is limited. For the matter of the topic I added a counter for the swap() code in the insertion fallback algorithm, and for a set of 2048 integers I never saw the swap() count exceed this number. For a pre-sorted array, values around ~2047 and reverse sorted ~2043. For random input far less. Citing the document "bentley93engineering.pdf", a footnote says: <cite> Of course, quadratic behavior is still possible. One can generate fiendish inputs by bugging Quicksort: Con- sider key values to be unknown initially. In the code for selecting a partition element, assign values in increas- ing order as unknown keys are encountered. In the partitioning code, make unknown keys compare high. </cite> Did anyone try to generate such a fiendish set of data, and see how quadratic the FreeBSD's qsort() becomes? Another thread, possibly related: http://postgresql.nabble.com/Why-do-we-still-perform-a-check-for-pre-sorted-input-within-qsort-variants-td5746526.html --HPS
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5715079B.9010408>