Borland C++ reference

qsort

raw OCR

Sorts using the quicksort algorithm.

Defined in header <stdlib.h>

Syntax

#include <stdHb.h>
void qsort(void  *hase, size_t nelem, size_t width,
            int (*fcmp){const void *, const void *));

Portability

DOSUNIXWindowsANSI CC++ only
■■■■

Remarks

qsort is an implementation of the "median of three" variant of the quicksort algorithm, qsort sorts the entries in a table by repeatedly calling the user-defined comparison function pointed to by fcmp.

■ base points to the base (0th element) of the table to be sorted. ■ nelem is the number of entries in the table. ■ width is the size of each entry in the table, in bytes.

*fcmp, the comparison function, accepts two arguments, eleml and eleml, each a pointer to an entry in the table. The comparison function compares each of the pointed-to items {*eleml and '^eleml), and returns an integer based on the result of the comparison.

* eleml < * eleml fcmp returns an integer < 0 *eleml == '^eleml fcmp returns 0 * eleml > *elem2 fcmp returns an integer > 0

In the comparison, the less-than symbol (<) means the left element should appear before the right element in the final, sorted sequence. Similarly, the greater-than (>) symbol means the left element should appear after the right element in the final, sorted sequence.

Return value

None.

See also

Example

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int sort_function ( const void *a, const void *b) ;

char list[5][4] =  { "cat", "car", "cab", "cap", "can" };

int main (void)
{
   int  x;
   qsort((void *)list,  5, sizeof(list[0]), sort_function);
   for (x = 0; X < 5; x++)
      printf ("%s\n", list[x]);
   return 0;
}
int sort_function (const void *a, const void *b)
{
   return( strcmp( (char *)a,(char *)b) ) ;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.

Extraction notes

  • header `stdHb.h` read as `stdlib.h`