home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c220 / 4.ddi / DEMOS / QSORT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-16  |  2.4 KB  |  111 lines

  1. /*********************************/
  2. /*          quicksort.c          */
  3. /*                               */
  4. /*********************************/
  5.  
  6. #include <system.cf>    /* For High C timings. */
  7. #include <stdio.h>
  8.  
  9. #define  MAXNUM   1000
  10. #define  COUNT    100
  11. #define  MODULUS  ((long)0x20000)
  12. #define  C        13849L
  13. #define  A        25173L
  14.  
  15. long  seed = 7L;
  16.  
  17. long  random();
  18.  
  19. long  buffer[MAXNUM]={0};
  20.  
  21.    long  timeticks();         /* Replace timeticks() with your */
  22.                               /* own system timing routine.    */
  23. void main()
  24.    {
  25.    int i,j;
  26.    long  temp;
  27.    long  starttime,stoptime,benchtime;
  28.    long  nulltime;
  29.    starttime = timeticks();
  30.    stoptime  = timeticks();
  31.    nulltime  = stoptime - starttime;
  32.  
  33.    printf("   Filling Array and Sorting %d Times\n",COUNT);
  34.  
  35.    starttime = timeticks();
  36.  
  37.    for(i=0;i<COUNT;++i)
  38.    {
  39.       for(j=0;j<MAXNUM;++j)
  40.       {
  41.          temp=random(MODULUS);
  42.          if(temp<0L)
  43.             temp=(-temp);
  44.          buffer[j]=temp;
  45.       }
  46.  
  47.       /*
  48.       printf(" Buffer Full, Iteration %d\n",i);
  49.       */
  50.  
  51.       quick(0,MAXNUM-1,buffer);
  52.    }
  53.       stoptime  = timeticks();
  54.       benchtime = stoptime - starttime - nulltime;
  55.  
  56.       printf("   Done With Iteration %d\n",i);
  57.       printf("   Hundredths of a second = %ld\n",benchtime);
  58.  
  59.    }
  60.  
  61.    quick(lo,hi,base)
  62.       int   lo,hi;
  63.       long  base[];
  64.  
  65.    {
  66.       int i,j;
  67.       long  pivot,temp;
  68.  
  69.       if(lo<hi)
  70.       {
  71.  
  72.          for(i=lo,j=hi,pivot=base[hi];i<j;)
  73.          {
  74.          while( i<hi && base[i]<= pivot)
  75.             ++i;
  76.          while( j>lo && base[j]>= pivot)
  77.             --j;
  78.          if(i<j)
  79.             {
  80.             temp=base[i];
  81.             base[i]=base[j];
  82.             base[j]=temp;
  83.             }
  84.          }
  85.       temp=base[i];
  86.       base[i]=base[hi];
  87.       base[hi]=temp;
  88.  
  89.       quick(lo,i-1,base);
  90.       quick(i+1,hi,base);
  91.       }
  92.    return 0;
  93.    }
  94.  
  95.    long  random(size)
  96.       long  size;
  97.       {
  98.       seed=seed*A+C;
  99.       return(seed % size);
  100.       }
  101.  
  102.  
  103.  
  104. /******************************************************************/
  105. /* Function returns number of Time Ticks (100*sec) since midnight. */
  106. /* Replace with your own routine or function.                     */
  107. /******************************************************************/
  108. long  timeticks()
  109. {    return clock();
  110. }
  111.