DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

qsort(S-osr5)


qsort -- quicker sort

Syntax

cc . . . -lc

#include <stdlib.h>

void qsort (void *base, size_t nel, size_t width, int (*compar) (const void *, const void *));

Description

The qsort function is an implementation of the quicker-sort algorithm. It sorts a table of data in place.

The arguments are:


base
pointer to the element at the base of the table.

nel
number of elements in the table.

width
size, in bytes, of each element.

compar
name of the comparison function, which is called with two arguments that point to the elements being compared. The comparison function must return an integer less than, equal to, or greater than zero, according to whether the first argument is to be considered as less than, equal to, or greater than the second argument.

Notes

The pointer to the base of the table should be of type pointer-to-element, and cast to type pointer-to-void.

The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared.

The order in the output of two items which compare as equal is unpredictable.

Example

   /* Comparison rtn for qsort on array of ints - note casting */
   int
   intcmp(const void *aa, const void *bb)
   {
       int *a = (int *) aa;
       int *b = (int *) bb;
   

return(*a - *b); /* ascending order */ /* return(*b - *a); /* descending order */ }

...

int list[SIZE]; size_t nelems = SIZE; /* or sizeof(list)/sizeof(int) */ ... qsort((void*)list, nelems, sizeof(int), intcmp);

See also

bsearch(S-osr5), lsearch(S-osr5), sort(C), string(S-osr5)

Standards conformance

qsort is conformant with:

X/Open Portability Guide, Issue 3, 1989 ;
ANSI X3.159-1989 Programming Language -- C ;
IEEE POSIX Std 1003.1-1990 System Application Program Interface (API) [C Language] (ISO/IEC 9945-1) ;
and NIST FIPS 151-1 .


© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005