/****************************************************************************/
/*                                                                          */
/* Sample Program 03 : SAMPLE03.C                                           */
/*                                                                          */
/* COPYRIGHT:                                                               */
/* ----------                                                               */
/* Copyright (C) International Business Machines Corp., 1991, 1993.         */
/*                                                                          */
/* DISCLAIMER OF WARRANTIES:                                                */
/* -------------------------                                                */
/* The following [enclosed] code is sample code created by IBM              */
/* Corporation.  This sample code is not part of any standard IBM product   */
/* and is provided to you solely for the purpose of assisting you in the    */
/* development of your applications.  The code is provided "AS IS",         */
/* without warranty of any kind.  IBM shall not be liable for any damages   */
/* arising out of your use of the sample code, even if they have been       */
/* advised of the possibility of such damages.                              */
/*                                                                          */
/****************************************************************************/

#include <stdio.h>
#include "sample03.h"
/* #pragma export(selection,,1) */
size_t nSwaps;
size_t nCompares;

/* Swap two integers.                                                       */

static void swap(int *x, int *y)
{
   int temp;
   temp = *x;
   *x = *y;
   *y = temp;
   ++nSwaps;
   return ;
}

/* Compare two integers.                                                    */

static int compare(int x, int y)
{
   ++nCompares;
   return (x-y);
}

/* Use Bubble Sort to sort an array of integers in increasing order.        */

void bubble(int array[], size_t nNumElements)
{
   size_t i,j;                         /* array indices                     */
   nSwaps = nCompares = 0;
   for (i = 0; i < nNumElements-1; ++i) {
      for (j = nNumElements-1; j > i; --j) {
         if (compare(array[j], array[j-1]) < 0)
            swap(&array[j], &array[j-1]);
      }
   }
   return ;
}
#ifdef   STATIC_LINK
#pragma  handler(      bubble )
#endif

/* Use Insertion Sort to sort an array of integers in increasing order.     */

void insertion(int array[], size_t nNumElements)
{
   size_t i,j;                         /* array indices                     */
   nSwaps = nCompares = 0;
   for (i = 1; i < nNumElements; ++i) {
      for (j = i; j && (compare(array[j], array[j-1]) < 0); --j)
         swap(&array[j], &array[j-1]);
   }
   return ;
}
#ifdef   STATIC_LINK
#pragma  handler(      insertion )
#endif

/* Use Selection Sort to sort an array of integers in increasing order.     */

void selection(int array[], size_t nNumElements)
{
   size_t i,j,lowindex;                /* array indices                     */
   int lowkey;                         /* temporary storage for lowest key
                                          value                             */
   nSwaps = nCompares = 0;
   for (i = 0; i < nNumElements-1; ++i) {
      lowindex = i;
      lowkey = array[i];
      for (j = i+1; j < nNumElements; ++j) {
         if (compare(array[j], lowkey) < 0) {
            lowkey = array[j];
            lowindex = j;
         }
      }
      if (lowindex != i)
         swap(&array[i], &array[lowindex]);
   }
   return ;
}
#ifdef   STATIC_LINK
#pragma  handler(      selection )
#endif

/* List the integers in an arrary.                                          */

void list(int array[], size_t nNumElements)
{
   size_t i;
   for (i = 0; i < nNumElements; ++i)
      printf("%i ", array[i]);
   putchar('\n');
   return ;
}
#ifdef   STATIC_LINK
#pragma  handler(      list )
#endif
