QSORT - sort an array.

(ANSI Standard)

Usage:

#include <stdlib.h>
qsort( s, N, size, compare );

Where:

void *s;
points to the array that is to be sorted.
size_t N;
is the number of elements in the array.
size_t size;
is the size of each individual element.
int (*compar)(const void *,const void *);
points to a function that should be used to compare the array elements. This function must take two arguments, each of which is a pointer to an array element. It returns a zero if the elements are equal, a negative number if the first argument is less than the second, and a positive number if the first argument is greater than the second. This function allows "qsort" to determine the order in which to sort the array.

Description:

"qsort" sorts the elements of an array using the comparison function indicated by "compar".

For example, when sorting strings, the following function is a good choice for "compar".

scmp( void *sp1, void *sp2 )
{
    return( strcmp(*(char **)sp1, *(char **)sp2) );
}

The casting operation is necessary because of the requirements of "strcmp".

Notes:

This function is called "qsort" because its original implementation used a "quicksort" algorithm. For reasons of efficiency, the algorithm has since been changed to a shell-sort; however, the name has been retained for compatibility with older programs. Since the way of calling "qsort" will not change, the actual sorting algorithm used will not be relevant for most users.

See Also:

expl c lib strcmp

Copyright © 1996, Thinkage Ltd.