/* primer56.c - ugradjeni qsort u MS QickC: * primer QSORT.C * ilustruje; srand rand qsort */ #include #include #include #include #define ASIZE 1000 unsigned array[ASIZE]; /* Macro to get a random integer within a specified range */ #define getrandom( min, max ) ((rand() % (int)(((max)+1) - (min))) + (min)) /* Must be declared before call */ int cmpgle( unsigned *elem1, unsigned *elem2 ); void main() { unsigned i, *result, elements = ASIZE; /* Seed the random number generator with current time. */ srand( (unsigned)time( NULL ) ); /* Build a random array of integers. */ for( i = 0; i < ASIZE; i++ ) array[i] = getrandom( 1, ASIZE ); /* Sort array using Quicksort algorithm. * Prototip: * void qsort( void *base, size_t num, size_t width, * int (*compare)( void *elem1, void *elem2 ) ); */ qsort( (void *)array, (size_t)ASIZE, sizeof( int ), cmpgle ); for( i = 0; i < ASIZE; i ++ ) { printf( "%5u", array[i] ); if( !(i % 15) ) printf( "\n" ); } } /* Compares and returns greater than (1), less than (-1), or equal to (0). */ int cmpgle( unsigned *elem1, unsigned *elem2 ) { if( *elem1 > *elem2 ) return 1; else if( *elem1 < *elem2 ) return -1; else return 0; }