/* simple qsort demo */ #include #include int comp( const void *p1, const void *p2 ); /* function to compare for qsort */ main() { int i, n, key, *result; int a[] = { 27, 56, 13, 9, 32, 5, 11, 74, 0 }; n = sizeof( a ) / sizeof( a[ 0 ] ); printf( "Data is...\n" ); for( i = 0; i < n; i++ ) printf( "%5d%5d\n", i, a[ i ] ); qsort( a, /* address of array to be sorted */ n, /* number of elements in array */ sizeof( a[0] ), /* size of an element */ comp ); /* function to compare */ key = 9; result = (int *)bsearch( &key, /* address of key element */ a, /* address of array to be searched */ n, /* number of elements in array */ sizeof(a[0]), /* size of an element */ comp); /* function to compare */ if (result == NULL) printf("Not found.\n"); else printf("Found: %d\n", *result); return( 0 ); } int comp( const void *p1, const void *p2 ) { return( *(int *)p1 - *(int *)p2 ); }