home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!secapl!Cookie!frank
- From: frank@Cookie.secapl.com (Frank Adams)
- Subject: Re: Making qsort type-safe
- Message-ID: <1992Nov09.155723.112061@Cookie.secapl.com>
- Date: Mon, 09 Nov 1992 15:57:23 GMT
- References: <1992Nov5.160048.15113@bcrka451.bnr.ca> <1992Nov6.202732.27007@mprgate.mpr.ca> <dak.721261218@messua>
- Organization: Security APL, Inc.
- Lines: 20
-
- In article <dak.721261218@messua> dak@messua.informatik.rwth-aachen.de (David Kastrup) writes:
- >walduck@mpr.ca (Andrew Walduck) writes:
- >Quicksort needs some tuning to get really good. Fisrt, you should use
- >the median 3 algorithm for choosing your pivot, and take the median of
- >first, middle, and last element. That way, presorted data will partition
- >perfectly. In addition, the O(n^2) worst case becomes REALLY improbable.
-
- An interesting fact: if the array is sorted exactly backwards, this method
- is still O(n^2). If you use median of 2nd, middle, and next to last, you
- avoid this problem. (You should be reverting to insertion sort for small
- sub-arrays, anyhow. This makes a significant difference, whatever the size
- of the initial array.)
-
- Also, when the object in the array is equal to the pivot, move it.
- Otherwise, you will get O(n^2) behavior when the data to be sorted is all
- the same.
-
- >If you are sorting linked lists, however, use a mergesort
-
- Absolutely.
-