/*
* Un exemple d'utilisation de
* la fonction qsort de la librairie
* standard.
* void qsort (void *base, size_t nmemb, size_t size,
              int (*compar)(const void *, const void *));
*/

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

#include "table.c"

// fonction de comparaison :
int cmp( const void *x, const void *y)
{ if ( *( (int*) x ) < *( (int*) y )  ) return 0;
  return 1;
}

void  test ( void )
  {
  int n, *T;
  printf("\nVerification : ");
  for ( n = pas; n < nmax; n += pas ) {
    printf("\ntaille:%d", n);
    T = randtable( n );
    printable( T, n);
    qsort(T, n , sizeof(int), cmp ); 
    printable( T, n);
    if( ! verification( T , n) )
           printf("\nErreur: Tableau non trié !");
    free( T );
  }  
  printf("\neot...\n");
  exit (0 ); 
  }


void testsortdata( void )
  {
  int n, *T;
  time_t deb, fin;
  double fc=0;
  printf("\nTemps de calcul table en ordre : ");
  pas = nmax / nblignes;
  for ( n = pas; n <= nmax; n += pas ) {
    T = sorttable( n );
    S = (int *) malloc(sizeof(int) * n); 
    deb = time( NULL );
    qsort(T, n , sizeof(int), cmp );
    fin = time( NULL );
    fc += difftime( fin, deb )/n/n;
    printf("\nn= %d duree= %.2f", n, difftime( fin, deb ));
    free( S ); free( T );
  }
  printf("\nfacteur cache : %e\n", fc/nblignes);
  exit(0); 
  }

void testranddata( void )
  {
  int n, *T;
  time_t deb, fin;
  double fc=0;  
  printf("\nTemps de calcul table aleatoire : ");
  for ( n = pas; n <= nmax; n += pas ) {
    T = randtable( n );
    S = (int *) malloc(sizeof(int) * n); 
    deb = time( NULL );
    qsort(T, n , sizeof(int), cmp );
    fin = time( NULL );
    fc = difftime( fin, deb )/n/log(n);
    printf("\nn= %d duree= %.2f", n, difftime( fin, deb ) ); 
    free( S ); free( T );
  }
  printf("\nfacteur cache : %e\n", fc/nblignes);
  exit(0); 
  }


int main (int argc ,char*argv[])
  {

  options( argc,argv );

  if ( check ) test ();
  if (   randopt ) testranddata(); 
  if ( ! randopt ) testsortdata();
 
  return 0; 
  }
