1 #define SIZE 10000000
   2 #define NB_TESTS 4
   3 #include <stdio.h>
   4 #include <sys/times.h>
   5 #include <time.h>
   6 
   7 void sort (int array[],int size);
   8 
   9 main( )
  10 {
  11   int i,t ;
  12   long duration[NB_TESTS];
  13   long duration_val;
  14   clock_t starttime,endtime ;
  15   int *array;
  16 
  17   for (t=0;t<NB_TESTS;t++)
  18   {
  19     starttime=clock();
  20     array=(int*)malloc(sizeof(int)*SIZE);
  21     for ( i = 0 ; i < SIZE ; i++ )
  22       array[i]=(i*7)%SIZE;
  23     sort(array,SIZE);
  24     free(array);
  25     endtime=clock();
  26     duration[t]=1000*((double)(endtime-starttime))/CLOCKS_PER_SEC;
  27     fprintf(stderr," %dms",duration[t]);
  28   }
  29   duration_val=duration[0];
  30   for (t=1;t<NB_TESTS;t++)
  31     if (duration_val>duration[t])
  32       duration_val=duration[t];
  33   printf("%d",duration_val);
  34 }
  35 
  36 static void quicksort ( int array[], int low, int high )
  37 {
  38   int pos  ;
  39   if ( low < high )
  40   {
  41     int item, i, j, t ;
  42     item = array[low] ;
  43     i = low ;
  44     j = high ;
  45     while ( i < j )
  46     {
  47       while ( array[j] > item )
  48         j = j - 1 ;
  49       while ( array[i] <= item  &&  i < j )
  50         i = i + 1 ;
  51       if ( i < j )
  52       {
  53         t = array[i] ;
  54         array[i] = array[j] ;
  55         array[j] = t ;
  56       }
  57     }
  58     pos = j ;
  59     t = array[low] ;
  60     array[low] = array[pos] ;
  61     array[pos] = t ;
  62 
  63 
  64     quicksort ( array, pos + 1, high ) ;
  65     quicksort ( array, low, pos - 1 ) ;
  66   }
  67 }
  68 
  69 void sort (int array[],int size)
  70 {
  71   quicksort ( array, 0, size-1 ) ;
  72 }