Using Quick Sort

Study the applications of function pointers.

We'll cover the following...

Example program

The code given below illustrates the use of function pointers.

Press + to interact
# include <stdio.h>
# include <stdlib.h>
int fun ( const void * , const void * ) ;
int main( )
{
int a[ ] = { 17, 6, 13, 12, 2 } ;
int i ;
qsort ( a, 5, sizeof( int ), fun ) ;
for( i = 0 ; i <= 4 ; i++ )
printf ( "%d\n", a[ i ] ) ;
}
int fun ( const void *x, const void *y )
{
int *xx, *yy ;
xx = ( int * ) x ;
yy = ( int * ) y ;
return ( *xx - *yy ) ;
}

Explanation

qsort( ) is a library function. It is being used here to arrange numbers in the array, a[ ], in ascending order.

The fourth parameter ...