home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / 8 < prev    next >
Encoding:
Text File  |  1992-04-14  |  1.9 KB  |  94 lines

  1. /* Simple test program:   bubble sort of a fixed table.     */
  2. /* This demonstrates some of the compiler's common-subexpression*/
  3. /* elimination capabilities.  For example, inspect the code    */
  4. /* generated for procedure Sort_array.    See the Programmer's    */
  5. /* Guide for how to request an assembly listing on your host.    */
  6.  
  7. /*
  8.  * sort.c -- For benchmarking, define -DBENCHMARK -DITERATIONS=x,
  9.  *   where x is the number of iterations desired.
  10.  */
  11.  
  12.  
  13. #include <time.h>
  14.  
  15. double second() {
  16.    return (double)(((double)(clock()))/((double)(CLOCKS_PER_SEC)));
  17.    }
  18.  
  19.  
  20. /* set the # of iterations as desired */
  21. #ifndef ITERATIONS
  22. #  define ITERATIONS 1
  23. #endif
  24. typedef unsigned char boolean;
  25.  
  26.  
  27. extern int printf(), puts();
  28.  
  29. void Sort_array(int Tab[],int Last) {
  30.    boolean Swap;
  31.    int Temp,I;
  32.    do {
  33.       Swap = 0;
  34.       for (I = 0; I<Last; I++)
  35.      if (Tab[I] > Tab[I+1]) {
  36.         Temp = Tab[I];
  37.         Tab[I] = Tab[I+1];
  38.         Tab[I+1] = Temp;
  39.         Swap = 1;
  40.         }
  41.       }
  42.    while (Swap);
  43.    }
  44.  
  45. int Tab[100];
  46.  
  47. void Print_array() {
  48.    int I,J;
  49.    puts("\nArray Contents:");
  50.    for (I=0; I<=9; I++) {
  51.       printf("%5d:",10*I);
  52.       for (J=0; J<=9; J++) printf("%5d",Tab[10*I+J]);
  53.       puts("\n");
  54.       }
  55.    }
  56.  
  57.  
  58. Init_array() {
  59.    int I,J,K;
  60.  
  61.    /* Initialize the table that will be sorted.         */
  62.    K = 0;
  63.    for (I = 9; I >= 0; I--)
  64.       for (J = I*10; J < (I+1)*10; J++)
  65.      Tab[K++] = J&1 ? J+1 : J-1;
  66.    }
  67.  
  68.  
  69. void main() {
  70.    double t1, t2, t;
  71.    int i;
  72.    t = 0;
  73. #ifndef BENCHMARK
  74.    Init_array();
  75.    Print_array();
  76. #endif
  77.    for (i = 0; i < ITERATIONS; i++) {
  78.      Init_array();
  79.      t1 = second(); 
  80.      Sort_array(Tab,99);
  81.      t2 = second();
  82.      t += t2 - t1;
  83.      }
  84. #ifdef BENCHMARK
  85.    printf("Run through bubble sort of 100 integers, # iterations: %d\n",
  86.           ITERATIONS);
  87.    printf("Time taken: %lf seconds\n", t);
  88. #else
  89.    Print_array();
  90. #endif
  91.    }
  92.  
  93.  
  94.