Path: utzoo!attcan!uunet!ginosko!gem.mps.ohio-state.edu!usc!cs.utexas.edu!mailrus!jarvis.csri.toronto.edu!torsqnt!tmsoft!masnet!canremote!brian.olender From: brian.olender@canremote.uucp (BRIAN OLENDER) Newsgroups: comp.lang.c Subject: Re: Looking for portable sort Message-ID: <89092304111605@masnet.uucp> Date: 23 Sep 89 00:35:00 GMT Organization: Canada Remote Systems Limited, Mississauga, ON, Canada Lines: 59 Subj: Looking for portable sort algorithm In article <1102@lakesys.UUCP> davek@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#include typedef int (*comp_t)(const void *, const void *); *-*-*-*-* Message continued in next message *-*-*-*-* --- * Via ProDoor 3.1aR