home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap07 / bubsort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-05  |  1.3 KB  |  60 lines

  1. /* bubsort.c  --  passing an array to a function  */
  2. /*                affects the original array      */
  3.  
  4. #define NUMINTS 6
  5.  
  6. extern void Bub_sort();
  7. extern void Triple();
  8.  
  9. main()
  10. {
  11.     int num = 2, i;
  12.     static int list[NUMINTS] = { 6, 5, 4, 3, 2, 1 };
  13.  
  14.     printf("num before Triple = %d\n", num);
  15.     Triple(num);
  16.     printf("num after Triple  = %d\n", num);
  17.     printf("list[0] before Triple = %d\n", list[0]);
  18.     Triple(list[0]);
  19.     printf("list[0] after Triple  = %d\n", list[0]);
  20.  
  21.     printf("Before sorting -> ");
  22.     for (i = 0; i < NUMINTS; ++i)
  23.         {
  24.         printf("%d ", list[i]);
  25.         }
  26.     printf("\n");
  27.  
  28.     Bub_sort(list);
  29.     printf("After sorting ->  ");
  30.     for (i = 0; i < NUMINTS; ++i)
  31.         {
  32.         printf("%d ", list[i]);
  33.         }
  34.     printf("\n");
  35.  
  36. }
  37.  
  38. void Triple(int x)  /* function doesn't affect original */
  39. {
  40.     x *= 3;
  41. }
  42.  
  43. void Bub_sort(int vals[NUMINTS]) /* function changes original */
  44. {
  45.     int i, j, temp;
  46.  
  47.     for (i = (NUMINTS - 1); i > 0; --i)
  48.         {
  49.         for (j = 0; j < i; ++j)
  50.             {
  51.             if (vals[j] > vals[j+1])
  52.                 {
  53.                 temp      = vals[j];
  54.                 vals[j]   = vals[j+1];
  55.                 vals[j+1] = temp;
  56.                 }
  57.             }
  58.         }
  59. }
  60.