<< Previous | Next >>

qsort

int qsort( char * base, unsigned n, unsigned s, int (*cmp) () );

Description

Quick sort with center pivot, stack control, and easy-to-change comparison method. This version sorts fixed-length data items. It is ideal for integers, longs, floats and packed string data without delimiters. Raw integers, longs, floats or strings may be sorted, however, the string sort is not efficient.

Parameters

base
Base address of the raw string data.

n
Number of blocks to sort.

s
Number of bytes in each block.

cmp
User-supplied compare routine for two block pointers, p and q, that returns an int with the same rules used by Unix strcmp(p,q):

= 0: Blocks p and q are equal
< 0: p is less than q
> 0: p is greater than q


Beware of using ordinary strcmp()--it requires a null at the end of each string.

Return value

0 if the operation is successful.

Library

SYS.LIB

Example - Sorts an array of integers.

int mycmp(int *p, int *q){ return (*p - *q);}
const int q[10] = {12,1,3,-2,16,7,9,34,-90,10};
const int p[10] = {12,1,3,-2,16,7,9,34,-90,10};
main() {
int i;
qsort(p,10,2,mycmp);
for(i=0;i<10;++i) printf("%d. %d, %d\n",i,p[i],q[i]);
}


Output from the above sample program:

0. -90,  12
1. -2, 1
2. 1, 3
3. 3, -2
4. 7, 16
5. 9, 7
6. 10, 9
7. 12, 34
8. 16, -90
9. 34, 10

Dynamic C Functions << Previous | Next >> rabbit.com