-

Difference between revisions of "Functions HI-TECH C QSORT"

From HI-TECH C for CP/M Fan WIKI(EN)
Jump to: navigation, search
(Created page with "<strong>QSORT</strong> ==SYNOPSIS== #include <stdlib.h> void qsort(void * base, size_t nel, size_t width, int (*func)()) ==DESCRIPTION <strong>Qsort</strong>() i...")
 
m
 
Line 8: Line 8:
  
  
==DESCRIPTION
+
==DESCRIPTION==
 
<strong>Qsort</strong>() is an implementation  of  the  quicksort  algorithm.  It  sorts an array of nel items, each of length
 
<strong>Qsort</strong>() 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.
 
width bytes, located contiguously in  memory  at  base.

Latest revision as of 23:02, 31 July 2017

QSORT

SYNOPSIS

#include  <stdlib.h>

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


DESCRIPTION

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;
}

sort_em()
{
    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.