NAME
qsort - quicker sort

SYNOPSIS
void qsort ((char *) base, nel, sizeof (*base), compar)
unsigned nel;
int (*compar)( );

DESCRIPTION
qsort is an implementation of the quicker-sort algorithm. It sorts a table of data in place.

The base points to the element at the base of the table. The pointer to the base of the table should be of type pointer-to-element, and cast to type pointer-to-character.

The nel argument is the number of elements in the table.

The compar element is the name of the comparison function which is called with two arguments that point to the elements being compared. It need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. The function must return an integer less than, equal to, or greater than zero based on whether the first argument is to be considered less than, equal to, or greater than the second.

EXAMPLE
The following routine will accept Points designated by clicking button 1 and then sort the array based on x and y coordinate values.

    #include <dmd.h>
    Point p[100];
    ptcompar(p, q)
    Point *p, *q;
    {
    	if ( p->y < q->y ) return -1;
    	if ( p->y == q->y ){
    		if ( p->x == q->x ) return 0;
    		if ( p->x < q->x ) return -1;
    		return 1;
    	}
    	return 1;
    }

    Point *
    sortpoints()
    {
    	int i;
    	request (MOUSE|KBD);
    	for( i = 0; i < 100;){
    		wait (MOUSE);
    		if( button1 () ){
    			p[i++] = mouse.xy;
    			sleep (10);
    			continue;
    		}
    		if( button3 () ) break;
    	}
    	qsort ((char *)p, i, sizeof(Point),
                   ptcompar);
    	return p;
    }

NOTES
The order in the output of two items which compare as equal is unpredictable.

SEE ALSO
bsearch(3L), lsearch(3L), str(3L).