From HI-TECH C for CP/M Fan WIKI(EN)
Jump to: navigation, search



#include  <stdlib.h>

void      qsort(void * base, size_t nel,
size_t width, int (*func)())


Qsort() is an implementation of the quicksort algorithm. It sorts an array of nel items, each of length width bytes, located contiguously in memory at base. Func is a pointer to a function used by qsort() to compare items. It calls func with pointers to two items to be compared. If the first item is considered to be greater than, equal to or less than the second then func() should return a value greater than zero, equal to zero or less than zero respectively.

static short    array[100];

#define SIZE  sizeof array/sizeof array[0]
a_func(p1, p2)
short * p1, * p2;
    return *p1 - *p2;

    qsort(array, SIZE,
          sizeof array[0], a_func);

This will sort the array into ascending values. Note the use of sizeof to make the code independent of the size of a short, or the number of elements in the array.