home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / compiler / sample03 / sort / sample03.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-20  |  3.8 KB  |  107 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Sample Program 03 : SAMPLE03.C                                           */
  4. /*                                                                          */
  5. /* COPYRIGHT:                                                               */
  6. /* ----------                                                               */
  7. /* Copyright (C) International Business Machines Corp., 1991, 1993.         */
  8. /*                                                                          */
  9. /* DISCLAIMER OF WARRANTIES:                                                */
  10. /* -------------------------                                                */
  11. /* The following [enclosed] code is sample code created by IBM              */
  12. /* Corporation.  This sample code is not part of any standard IBM product   */
  13. /* and is provided to you solely for the purpose of assisting you in the    */
  14. /* development of your applications.  The code is provided "AS IS",         */
  15. /* without warranty of any kind.  IBM shall not be liable for any damages   */
  16. /* arising out of your use of the sample code, even if they have been       */
  17. /* advised of the possibility of such damages.                              */
  18. /*                                                                          */
  19. /****************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include "sample03.h"
  23. size_t _Export nSwaps;
  24. size_t _Export nCompares;
  25.  
  26. /* Swap two integers.                                                       */
  27.  
  28. static void swap(int *x, int *y)
  29. {
  30.    int temp;
  31.    temp = *x;
  32.    *x = *y;
  33.    *y = temp;
  34.    ++nSwaps;
  35.    return ;
  36. }
  37.  
  38. /* Compare two integers.                                                    */
  39.  
  40. static int compare(int x, int y)
  41. {
  42.    ++nCompares;
  43.    return (x-y);
  44. }
  45.  
  46. /* Use Bubble Sort to sort an array of integers in increasing order.        */
  47.  
  48. void _Export bubble(int array[], size_t nNumElements)
  49. {
  50.    size_t i,j;                         /* array indices                     */
  51.    nSwaps = nCompares = 0;
  52.    for (i = 0; i < nNumElements-1; ++i) {
  53.       for (j = nNumElements-1; j > i; --j) {
  54.          if (compare(array[j], array[j-1]) < 0)
  55.             swap(&array[j], &array[j-1]);
  56.       }
  57.    }
  58.    return ;
  59. }
  60.  
  61. /* Use Insertion Sort to sort an array of integers in increasing order.     */
  62.  
  63. void _Export insertion(int array[], size_t nNumElements)
  64. {
  65.    size_t i,j;                         /* array indices                     */
  66.    nSwaps = nCompares = 0;
  67.    for (i = 1; i < nNumElements; ++i) {
  68.       for (j = i; j && (compare(array[j], array[j-1]) < 0); --j)
  69.          swap(&array[j], &array[j-1]);
  70.    }
  71.    return ;
  72. }
  73.  
  74. /* Use Selection Sort to sort an array of integers in increasing order.     */
  75.  
  76. void _Export selection(int array[], size_t nNumElements)
  77. {
  78.    size_t i,j,lowindex;                /* array indices                     */
  79.    int lowkey;                         /* temporary storage for lowest key
  80.                                           value                             */
  81.    nSwaps = nCompares = 0;
  82.    for (i = 0; i < nNumElements-1; ++i) {
  83.       lowindex = i;
  84.       lowkey = array[i];
  85.       for (j = i+1; j < nNumElements; ++j) {
  86.          if (compare(array[j], lowkey) < 0) {
  87.             lowkey = array[j];
  88.             lowindex = j;
  89.          }
  90.       }
  91.       if (lowindex != i)
  92.          swap(&array[i], &array[lowindex]);
  93.    }
  94.    return ;
  95. }
  96.  
  97. /* List the integers in an arrary.                                          */
  98.  
  99. void _Export list(int array[], size_t nNumElements)
  100. {
  101.    size_t i;
  102.    for (i = 0; i < nNumElements; ++i)
  103.       printf("%i ", array[i]);
  104.    putchar('\n');
  105.    return ;
  106. }
  107.