qsort()

Syntax
  #include <stdlib.h>

  
  void *qsort(const void *array,

  
              size_t n,

  
              size_t size,

  
              cmp_func cmp);

  
Description

qsort() sorts the array according to the ordering implemented by the comparison function. It calls the comparison function cmp() with two pointers to array elements. Thus, the type cmp_func() can be declared as:

typedef int (*cmp_func)(const void *key,

const void *other);

The comparison function returns an integer according to the following table:

Table 1. Return value from the comparison function, cmp_func()
Key element value Return value
less than the other one less than zero (negative)
equal to the other one zero
greater than the other one greater than zero (positive)

The arguments to qsort() are listed in the following table:

Table 2. Possible arguments to the sorting function, qsort()
Argument Name Meaning
array A pointer to the beginning (i.e., the first element) of the array to be sorted
n The number of elements in the array
size The size (in bytes) of one element in the table
cmp() The comparison function
Note: Make sure the array contains elements of equal size.