Re: attempt at qsort implementation

From: Gordon Burditt (gordonb.jgi3m_at_burditt.org)
Date: 09/29/04


Date: 29 Sep 2004 06:01:55 GMT


>I had some spare time and decided to try to implement the standard library
>qsort. This is a pretty simpleminded attempt, but it seems to be working. I
>have to point out that efficiency is not at all an issue for me, and this is
>purely a toy, so to speak. I'm quite aware that this is not the quickest way
>to implement the quicksort algorithm, but I basically just wanted to try to
>make a "generic function".

qsort() is not required to implement any particular sort algorithm.
It's just supposed to sort. It's not specified HOW. A bubble sort
is not an invalid implementation. A recursive bogosort [*] (which
operates in time equivalent to O(e**infinity)) is probably an invalid
implementation since it NEVER finishes.

>I tested it with a few arrays of structures
>containing all sorts of types (this, ofcourse, doesn't mean that it's
>correct, even by a long shot :). I would appreciate any and all constructive
>comments. The code below is a snippet. All the necessary headers are
>included and uchar is a typedef for unsigned char.
>Thank you.

If count == 1, the algorithm does a compare anyway. I know you
weren't going for real efficiency here, but this seems a bit
strange.

If sz == 0, various wierd and obnoxious things will happen, but
the caller IS asking for it.

It seems like this algorithm could end up recursing a lot.
This could be a problem with large amounts of data.

One thing worth checking here is how well your algorithm will operate
with a bogus compare routine (e.g. one which always returns -1, or
one for which cmp(a,b) is not guaranteed to equal -cmp(b,a) ). Some
implementations end up going outside the passed-in array in such
circumstances.

[*] Technical note: a bogosort operates by creating all possible
orders of the sorted array, determining how many elements are out
of order, and returns the order with the least number of elements
out of order by sorting them and returning the first one. In other
words, it turns the problem of sorting N elements into one of sorting
N! elements.

A recursive bogosort uses bogosort to sort the possible orders by
number of elements out of order, thereby turning the problem of
sorting N elements into one of sorting N!!!!!!!!!!!!!!!!!!!!!!!!!!....
elements. Bogosorts typically include other performance-wasting
code such as:
        while (malloc(INT_MAX)) fork();
since they will never finish anyway.

                                Gordon L. Burditt



Relevant Pages

  • Re: Sorting algorithm when comparison is heavy
    ... optimal way of sorting such elements (in other words, ... I would consider some good sort algorithm like quicksort together ... You suggest using pointers, so I think the above applies. ...
    (comp.programming)
  • Re: sort on more than one key?
    ... I can sort on one key no problem.But what ... > sorting on more than one key, for example by last name then by first name. ... unstable) and in the compare function code the following for any pair ... key using a stable algorithm such as a nice "bubble" sort ...
    (comp.programming)
  • Re: lottery number thing problem
    ... Chris Theis wrote: ... (look for random_shuffle and sort). ... If you want to do the actual sorting yourself you will need a sorting ... algorithm. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: lottery number thing problem
    ... Chris Theis wrote: ... (look for random_shuffle and sort). ... If you want to do the actual sorting yourself you will need a sorting ... algorithm. ...
    (comp.lang.cpp)
  • Re: "Sorting" assignment
    ... algorithm such as tbe bubble sort should be given a free pass because ... I would tailor which algorithm to start with by how the student thinks ... If you only learn the beautiful side of programming, ...
    (comp.programming)