/****Copyright (c) 1997-2009 All rights reserved
 ****A. V. Gerbessiotis 
 ****Permission to use, copy, modify, and distribute this software,
 ****and to incorporate it, in whole or in part, into 
 ****other software,is hereby granted without fee, provided that
 ****  (1) the above copyright notice and this permission 
 ****      notice appear in all copies of the source code, and 
 ****      the above copyright notice appear in clearly visible 
 ****      form on all supporting documentation and distribution 
 ****      media, and
 ****     (2) any redistribution of the software, in original
 ****         or modified form, be without fee and subject to
 ****         these same conditions.
 ****   No guarantee, whatsoever, is offered that the code works as
 ****   advertised or is absent of any, even damaging,
 ****   side-effects. Use this code at your own personal risk.
 ****/

#include "ssrt.h"
#include "avgmain.h"

#define N_SORT_ALGS 18
#define DEBUG 0

int compare(const void *x,const void *y)
{
	return(* ((data*)x)-*((data*)y));
}

int main(int argc,char *argv[]) {
  int i,j,k,n,runs,type;
  data *ptr,*bptr;
  double elapsed;
  void (*gfunc[N_SORT_ALGS])() = {
	qsort,    g01ins,   g02ins,   g04sel,  g05bub,  
        g06bins,  g07shake, g08shell, g11ins,  g12ins,
        g14sel,   g21ins,   g24sel,   g25bub,  g27shake,
        g28shell, g77shake, g78shell
  };
  char *labels[N_SORT_ALGS]   = {
	"qsort",    "g01ins",   "g02ins",   "g04sel",  "g05bub",  
        "g06bins",  "g07shake", "g08shell", "g11ins",  "g12ins",
        "g14sel",   "g21ins",   "g24sel",   "g25bub",  "g27shake",
        "g28shell", "g77shake", "g78shell"
  };

  void (*GTYPESORT)(void *,int,int,int (*) (const void *,const void *));


  n=1000;runs=1;
  if (( argc <=1  ) || (argc > 4)) {
    printf("Usage: %s size runs type \n",argv[0]);
    return(1);
  } 
  else {
    n = atoi(argv[1]);
    runs=atoi(argv[2]);
    type=atoi(argv[3]);
   }

   ptr = (int*) Malloc(n*sizeof(data),"ptr  ");
   bptr= (int*) Malloc(n*sizeof(data),"bptr ");
   memset( ptr,0,n*sizeof(data ));
   memset(bptr,0,n*sizeof(data ));
   srandom(21+1001*11);
   srandom(21+1001*11);
   for(i=0;i<n;i++)  {
    if  ( type==1 ) {
     bptr[i]=ptr[i]=(data) (random()&MODULO_UINT);
    }
    if  ( type==2 ) {
     bptr[i]=ptr[i]=(int) ((n)*(random()/((double)INT_MAX)));
    }
    if  ( type==3 ) {
     bptr[i]=ptr[i]=(int) 10;
    }
    if  ( type==4 ) {
     bptr[i]=ptr[i]=(int) i;
    }
    if  ( type>=5 ) {
     bptr[i]=ptr[i]=(int) n-i+1;
    }
   }

   for(k=0;k<N_SORT_ALGS;k++) {
     GTYPESORT=gfunc[k];
     WRITELINE1; WRITE1;
     GTYPESORT(ptr,n,sizeof(data),compare);
     WRITE2; WRITELINE2; CHECKLINE;
     printf("%10s generic: Elapsed time is %10.8f\n",labels[k],elapsed/runs);
   }

   Free((void *) bptr,n*sizeof(data),"bptr");
   Free((void *) ptr,n*sizeof(data),"ptr");

  return(0);
}
