void  qsort ( void * base, size_t num, size_t width, int (*fncompare)(const void *, const void *) );

Sort using quicksort algorith.
  This function uses an implementation of the quicksort algorithm to sort the num elements of an array pointed by base, each element has the specified width in bytes.
  The method used to compare each pair of elements is provided by the caller to this function with fncompare parameter, that is a function called one or more times during the sort process.


Pointer to the first element of the array where the sorting process has to be performed.
Number of elements in the array pointed by base.
Width of each element in the array.
Function that compares two elements. This should be provided by the caller to this function and must follow or be casted to a declaration like:
int fncompare (const void * elem1, const void * elem2 );
The function should receive two parameters (elem1 and elem2) that are pointers to elements, and should return an int value with the result of comparing them:
return valuedescription
<0*elem1 goes before *elem2
0*elem1 == *elem2
>0*elem1 goes after *elem2

Return Value.

  Defined in ANSI-C.


/* qsort example */
#include <stdio.h>
#include <stdlib.h>

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
  return ( *(int*)a - *(int*)b );

main ()
  int * pItem;
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
    printf ("%d ",values[n]);
  return 0;
10 20 25 40 90 100

See also.
  bsearch, lsearch

© The C++ Resources Network, 2000