Looking for portable sort

BRIAN OLENDER brian.olender at canremote.uucp
Sat Sep 23 10:35:00 AEST 1989


Subj: Looking for portable sort algorithm
 
In article <1102 at lakesys.UUCP> davek at lakesys.UUCP (Dave Kraft) writes:
>Hi,
>I'm looking for a sort algorithm that is portable
>between Turbo C 2.0 and Xenix.
 
There are many different sort algorithms; and equally as many
applications for same. A common application is to sort an array of
pointers or structures (or other objects) in memory; the ANSI standard
routine qsort() is designed for this.
 
To call qsort, you must provide as parameters:
 
(1) a pointer to the array of elements to be sorted.
 
(2) the number of elements in the array.
 
(3) the width of each element. [use the macro "sizeof()" to determine
this]
 
(4) a pointer to a comparison function which in some way compares the
elements which it's arguments point to. [Returning zero if the elements
are equal; positive if the first argument evaluates greater than the
second; negative otherwise].
 
A useful reference for this and many other library functions is "The
Waite Group's Turbo C Bible" by Barkakati; published by Howard W. Sams &
Company.
 
Here is a heapsort which should be reasonably portable between ANSI
compilers. Heapsort is sometimes preferred over quicksort since it has
very much better worst case performance (although quicksort wins where
average performance is the criterion).
 
======== cut here =========
 
/*  hsort  - a Heap Sort Function in 'C'
 *
 *  Released to the public domain 1989 by B. P. Olender
 *
 *  Calling Convention:   Same as ANSI library function qsort()
 *
 *  References: Donald E. Knuth, "The Art of Computer Programming
 *                  - Volume 3 - Sorting and Searching";
 *                  Addison-Wesley
 *
 *              William H. Press et al, "Numerical Recipes in C";
 *                  Cambridge Press
 */
 
#include <stdlib.h>
#include <string.h>
 
typedef int (*comp_t)(const void *, const void *);
 
*-*-*-*-* Message continued in next message *-*-*-*-*
---
 * Via ProDoor 3.1aR 



More information about the Comp.lang.c mailing list