home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / samples / sam03l / sample03.c__ / SAMPLE03.C
Encoding:
C/C++ Source or Header  |  1992-04-01  |  4.1 KB  |  145 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* Sample Program 03 : SAMPLE03.C                                             */
  4. /*                                                                            */
  5. /* COPYRIGHT:                                                                 */
  6. /* ----------                                                                 */
  7. /* Copyright (C) International Business Machines Corp., 1991, 1992.           */
  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.  
  24. size_t nSwaps;
  25. size_t nCompares;
  26.  
  27. /* Swap two integers.                                                         */
  28.  
  29. static void swap( int *x , int *y )
  30.    {
  31.    int temp;
  32.  
  33.    temp = *x;
  34.    *x   = *y;
  35.    *y   = temp;
  36.    ++nSwaps;
  37.  
  38.    return;
  39.    }
  40.  
  41. /* Compare two integers.                                                      */
  42.  
  43. static int compare( int x, int y )
  44.    {
  45.    ++nCompares;
  46.    return( x - y );
  47.    }
  48.  
  49. /* Use Bubble Sort to sort an array of integers in increasing order.          */
  50.  
  51. void bubble( int array[], size_t nNumElements )
  52.    {
  53.    size_t i, j;   /* array indices */
  54.  
  55.    nSwaps = nCompares = 0;
  56.  
  57.    for ( i = 0; i < nNumElements - 1; ++i )
  58.       {
  59.       for ( j = nNumElements - 1; j > i; --j )
  60.          {
  61.          if ( compare( array[ j ], array[ j - 1 ] ) < 0 )
  62.             swap( &array[ j ], &array[ j - 1 ] );
  63.          }
  64.       }
  65.  
  66.    return;
  67.    }
  68.  
  69. #ifdef STATIC_LINK
  70.    #pragma handler( bubble )
  71. #endif
  72.  
  73. /* Use Insertion Sort to sort an array of integers in increasing order.       */
  74.  
  75. void insertion( int array[], size_t nNumElements )
  76.    {
  77.    size_t i, j;   /* array indices */
  78.  
  79.    nSwaps = nCompares = 0;
  80.  
  81.    for ( i = 1; i < nNumElements; ++i )
  82.       {
  83.       for ( j = i; j && ( compare( array[ j ], array[ j - 1 ] ) < 0 ); --j )
  84.          swap( &array[ j ], &array[ j - 1 ] );
  85.       }
  86.  
  87.    return;
  88.    }
  89.  
  90. #ifdef STATIC_LINK
  91.    #pragma handler( insertion )
  92. #endif
  93.  
  94. /* Use Selection Sort to sort an array of integers in increasing order.       */
  95.  
  96. void selection( int array[], size_t nNumElements )
  97.    {
  98.    size_t i, j, lowindex;   /* array indices                          */
  99.    int lowkey;              /* temporary storage for lowest key value */
  100.  
  101.    nSwaps = nCompares = 0;
  102.  
  103.    for ( i = 0; i < nNumElements - 1; ++i )
  104.       {
  105.       lowindex = i;
  106.       lowkey = array[ i ];
  107.  
  108.       for ( j = i + 1; j < nNumElements; ++j )
  109.          {
  110.          if ( compare( array[ j ], lowkey ) < 0 )
  111.             {
  112.             lowkey = array[ j ];
  113.             lowindex = j;
  114.             }
  115.          }
  116.  
  117.       if ( lowindex != i )
  118.          swap( &array[ i ], &array[ lowindex ] );
  119.       }
  120.  
  121.    return;
  122.    }
  123.  
  124. #ifdef STATIC_LINK
  125.    #pragma handler( selection )
  126. #endif
  127.  
  128. /* List the integers in an arrary.                                            */
  129.  
  130. void list( int array[], size_t nNumElements )
  131.    {
  132.    size_t i;
  133.  
  134.    for ( i = 0; i < nNumElements ; ++i )
  135.       printf( "%i ", array[ i ] );
  136.  
  137.    putchar( '\n' );
  138.  
  139.    return;
  140.    }
  141.  
  142. #ifdef STATIC_LINK
  143.    #pragma handler( list )
  144. #endif
  145.