home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 5.ddi / MWHC.005 / X < prev    next >
Encoding:
Text File  |  1992-03-30  |  1.2 KB  |  51 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. #include <stdio.h>
  7.  
  8. typedef unsigned char boolean;
  9.  
  10. void Sort_array(int Tab[],int Last) {
  11.    boolean Swap;
  12.    int Temp,I;
  13.    do {
  14.       Swap = 0;
  15.       for (I = 0; I<Last; I++)
  16.      if (Tab[I] > Tab[I+1]) {
  17.         Temp = Tab[I];
  18.         Tab[I] = Tab[I+1];
  19.         Tab[I+1] = Temp;
  20.         Swap = 1;
  21.         }
  22.       }
  23.    while (Swap);
  24.    }
  25.  
  26. int Tab[100];
  27.  
  28. void Print_array() {
  29.    int I,J;
  30.    printf("\nArray Contents:\n");
  31.    for (I=0; I<=9; I++) {
  32.       printf("%5d:",10*I);
  33.       for (J=0; J<=9; J++) printf("%5d",Tab[10*I+J]);
  34.       printf("\n");
  35.       }
  36.    }
  37.  
  38. void main () {
  39.    int I,J,K;
  40.  
  41.    /* Initialize the table that will be sorted.         */
  42.    K = 0;
  43.    for (I = 9; I >= 0; I--)
  44.       for (J = I*10; J < (I+1)*10; J++)
  45.      Tab[K++] = J&1 ? J+1 : J-1;
  46.  
  47.    Print_array();
  48.    Sort_array(Tab,99);       /* Sort it.                */
  49.    Print_array();
  50.    }
  51.