Listing 3 Demonstrating Worst-Case qsort() Performance

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define ARRAY_SIZE 1000

int test_array[ARRAY_SIZE];

int compare( int *a, int *b )
{
   return( *a - *b );
}

main()
{
   int i;
   time_t start_time;
   time_t stop_time;
   
   for ( i=0; i<ARRAY_SIZE ; i++ )
      test_array[i] = rand();
   time( &start_time );
   qsort( test_array, ARRAY_SIZE, sizeof(int), compare );
   time( &stop_time );
   printf( "%f seconds elapsed.\n", difftime( stop_time, start_time));
   test_array[0] += 100;
   time( &start_time );
   qsort( test_array, ARRAY_SIZE, sizeof(int), compare );
   time( &stop_time );
   printf( "%f seconds elapsed.\n", difftime( stop_time, start_time));
   for (i=0 ; i<ARRAY_SIZE-1 ; i++ )
      if ( test_array[i] > test_array[i+1] )
          printf( "Mismatch at position %d\n", i );
}