1 /*  timmsrt.c    CCMATH mathematics library source code.
2  *
3  *  Copyright (C)  2000   Daniel A. Atkinson    All rights reserved.
4  *  This code may be redistributed under the terms of the GNU library
5  *  public license (LGPL). ( See the lgpl.license file for details.)
6  * ------------------------------------------------------------------------
7  */
8 /*
9     Time the sort of an array of real numbers using a merge sort.
10 
11     Input parameter:  size= n -> array of n double precision numbers
12 */
13 #include <time.h>
14 #include "ccmath.h"
main(int na,char ** av)15 void main(int na,char **av)
16 { struct llst *dat,*s0,*t;
17   double *x; int i,f,n;
18   clock_t st,en; unsigned int seed;
19   if(na!=2){ printf("para: size\n"); exit(1);}
20   n=atoi(*++av);
21   seed=(unsigned int)time(NULL); setnrml(seed);
22   printf("     Time Merge Sort\n");
23   printf("  %d input points\n",n);
24   x=(double *)calloc(n,sizeof(*x));
25   for(i=0; i<n ;) x[i++]=nrml();
26   dat=(struct llst *)calloc(n,sizeof(*dat));
27   st=clock();
28   for(i=0,s0=dat,t=s0; i<n ;++i){
29     t->pls=(char *)(x+i); t->pt=t+1; ++t;}
30   (t-1)->pt=NULL;
31   s0=msort(s0,n,dubcmp);
32   en=clock();
33   for(t=s0,f=0; t->pt!=NULL ;t=t->pt){
34     if(*(double *)(t->pt->pls) < *(double *)(t->pls)){ f=1; break;}
35    }
36   if(f) printf("  sort failed\n");
37   printf(" time : %.3f sec.\n",(double)(en-st)/(double)CLOCKS_PER_SEC);
38 }
39