From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: doligez@pa.dec.com, OCAML <caml-list@inria.fr>
Subject: Re: Sort.array easily degenerates
Date: Wed, 10 Mar 1999 14:58:02 +0100 [thread overview]
Message-ID: <19990310145802.51200@pauillac.inria.fr> (raw)
In-Reply-To: <199903092303.AA14481@six.pa.dec.com>; from doligez@pa.dec.com on Tue, Mar 09, 1999 at 03:03:05PM -0800
I have revised my Quicksort implementation based on that found in
glibc 2, which contains some clever optimizations. Interested parties
can see the code at
http://camlcvs.inria.fr/cgi-bin/cvsweb.out/ocaml/stdlib/sort.ml
The behavior on extreme situations (input already sorted) is now much
better.
> There's no way to implement Sedgewick's quicksort with the interface
> given in sort.mli. You'd need two comparison functions, one for ">="
> and one for "<=". That explains the degenerate case when all the
> elements are equal.
It's true that ">=" vs. ">" can make a big difference, but >= is
easily definable from <= : a >= b is b <= a, a > b is not(a <= b),
a < b is not(b <= a).
> And it degenerates on already-sorted data because you swap the pivot
> with the right-most element. As a consequence, one of the subarrays
> has its two highest elements in first and last positions, which makes
> the median-of-three degenerate. I think this bug is also in
> Sedgewick's pseudo-code.
Sedgewick doesn't even give pseudo-code for the "median of three"
heuristic, but the glibc implementation avoids this swap altogether.
> Also, you should recurse on the smallest subarray first, not the
> largest.
Good point.
> I vote for Shellsort.
Well, I tried it, and it's noticeably slower than quicksort by almost
a factor of two on random input. Perhaps because polymorphic array
access is quite expensive in OCaml.
- Xavier Leroy
next prev parent reply other threads:[~1999-03-10 16:59 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-03-06 0:27 Markus Mottl
1999-03-09 10:44 ` Xavier Leroy
1999-03-09 23:03 ` doligez
1999-03-10 13:58 ` Xavier Leroy [this message]
1999-03-10 0:28 ` Markus Mottl
[not found] <Xavier.Leroy@inria.fr>
1999-03-05 10:41 ` Objective Caml 2.02 Xavier Leroy
1999-03-05 13:34 ` Camlp4 2.02 Daniel de Rauglaudre
1999-03-05 15:11 ` Objective Caml 2.02 Pierpaolo Bernardi
1999-03-05 19:59 ` doligez
1999-03-11 3:06 ` Upgrade from OCaml 2.01 to OCaml 2.02 made things _slower_! Alexey Nogin
1999-03-11 9:44 ` Xavier Leroy
1999-03-11 23:59 ` Alexey Nogin
1999-03-13 13:40 ` Anton Moscal
1999-03-24 4:20 ` Alexey Nogin
1999-03-26 11:49 ` Anton Moscal
1999-04-06 2:06 ` Alexey Nogin
1999-04-06 7:53 ` Xavier Leroy
1999-03-11 23:42 ` List.filter in Ocaml 2.02 Alexey Nogin
1999-03-12 10:10 ` Wolfram Kahl
1999-03-12 18:18 ` Alexey Nogin
1999-03-13 2:43 ` David Monniaux
1999-03-12 17:01 ` Jean-Francois Monin
1999-03-12 18:41 ` Alexey Nogin
[not found] ` <199903121011.LAA27611@lsun565.lannion.cnet.fr>
1999-03-12 18:37 ` Alexey Nogin
1999-03-15 9:06 ` Jean-Francois Monin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=19990310145802.51200@pauillac.inria.fr \
--to=xavier.leroy@inria.fr \
--cc=caml-list@inria.fr \
--cc=doligez@pa.dec.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox