Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Sort.array easily degenerates
@ 1999-03-06  0:27 Markus Mottl
  1999-03-09 10:44 ` Xavier Leroy
  0 siblings, 1 reply; 25+ messages in thread
From: Markus Mottl @ 1999-03-06  0:27 UTC (permalink / raw)
  To: OCAML

Hello,

I have played around with the new functions and modules in the new
OCAML-release. Besides other things I have tested the new function for
sorting arrays (Sort.array).

I am not sure where the problem in the implementation is, but the
"qsort"-function, which is applied in "Sort.array" degenerates easily
on pre-sorted and/or non-unique data. E.g.:

-----  SNIP -----
let _ =
  let size = 5000 in
  let ar = Array.create size 0 in
  for i = 0 to (size-1) do ar.(i) <- i done;
  Sort.array (>=) ar
-----  SNIP -----

The array to be sorted is initialized with its index. Then it is sorted
with descending order.

Running the same test with larger arrays clearly shows that we encounter
the worst-case behaviour (n^2) of quicksort.

Even worse:

If we initialize the array with the same number, the time complexity
stays at its worst-case but with an even higher constant factor.

Initializing the array with random integers (of a large range) shows
that in such cases "Sort.array" does not perform this badly (actually
quite well).

I have compared this to "qsort" in "stdlib.h", the standard library of
C. It is faster than the OCAML-version, as was to be expected. But in
contrast to OCAML it behaves very nicely on low-entropy data.

E.g.: (C-Code):

-----  SNIP -----
#include <stdlib.h>

int int_comp (const void * a, const void * b) {
  return *(int *) a - *(int *) b;
}

const int size = 5000;

int main () {
  int ar[size];
  for (int i = 0; i < size; ++i) ar[i] = i;
  qsort (ar, size, sizeof(int), int_comp);
}
-----  SNIP -----

It would probably be a good idea to change the implementation of
"Sort.array" so as to make it more unlikely to encounter such worst-case
behaviour. Especially with data, where elements may occur more than once,
the current implementation performs really badly.

So here a question: has someone already written a quicksort-function
with in-place modification for arrays which demonstrates nicer behaviour?

Best regards,
Markus

-- 
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl



^ permalink raw reply	[flat|nested] 25+ messages in thread
[parent not found: <Xavier.Leroy@inria.fr>]

end of thread, other threads:[~1999-04-06 16:31 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-06  0:27 Sort.array easily degenerates Markus Mottl
1999-03-09 10:44 ` Xavier Leroy
1999-03-09 23:03   ` doligez
1999-03-10 13:58     ` Xavier Leroy
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

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