Friday, March 10, 2017

Programming Challenge 8.10 - Sorting Orders

/* Sorting Orders - This program uses two identical arrays of just eight
   integers. It displays the contents of the first array, then calls a
   function to sort the array using an ascending order bubble sort modified
   to print out the array contents after each pass of the sort. Next, the
   program displays the contents of the second array, then calls a function
   to sort the array using an ascending order selection sort modified to
   print out the array contents after each pass of the sort. */

#include "Utility.h"

void displayUnsorted(const int[], const int);
void bubbleSortAlg(int[], const int);
void selectionSortAlg(int[], const int);

int main()
{
   const int NUMELS = 8;

   int bubbleSort[NUMELS] = { 4, 8, 5, 3, 1, 2, 7, 6 };
   int selectionSort[NUMELS] = { 4, 8, 5, 3, 1, 2, 7, 6 };
 
   /* First the arrays are displayed, then the numbers are sorted,
   and the sorting process is displayed (bubble sort comes first,
   followed by selection sort) */
   cout << "\n\t\t BUBBLE SORT\n\n";

   displayUnsorted(bubbleSort, NUMELS);
   bubbleSortAlg(bubbleSort, NUMELS);

   cout << "\n\n\n\t\t SELECTION SORT\n\n";

   displayUnsorted(selectionSort, NUMELS);
   selectionSortAlg(selectionSort, NUMELS);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: displayUnsorted

   This function accepts bubbleSort, selectionSort and NUMELS
   containing the size of the arrays contents, as arguments.
   It displays the unsorted array, one after the other.
   ********************************************************** */

void displayUnsorted(const int unsortedArray[], const int NUMELS)
{
   cout << "\t\t UNSORTED LIST\n\n";

   cout << "\t\t";
   for (int index = 0; index < NUMELS; index++)
   {
      cout << " " << unsortedArray[index] << " ";
   }
   cout << "\n";
}

/* **********************************************************
   Definition: bubbleSortAlg

   This function accepts bubbleSort and NUMELS containing the
   number of elements in the array as arguments. After each
   sorting pass the content of the array is displayed.
   ********************************************************** */

void bubbleSortAlg(int bubbleSort[], const int NUMELS)
{
   int temp = 0,
       count = 0;

   bool swap = false;

   do
   {
      swap = false;

      for (count = 0; count < (NUMELS - 1); count++)
      {

         if (bubbleSort[count] > bubbleSort[count + 1])
         {
            temp = bubbleSort[count];
            bubbleSort[count] = bubbleSort[count + 1];
            bubbleSort[count + 1] = temp;
            swap = true;

            cout << "\n\t\t";
            for (count = 0; count < NUMELS; count++)
            {
               cout << " " << bubbleSort[count] << " ";
            }      
           }  
      }
   } while (swap);
}

/* **********************************************************
   Definition: selectionSortAlg

   This function accepts selectionSort and NUMELS containing
   the size of the array as arguments.
   While the numbers are sorted, the content of bubbleSort is
   displayed after each sorting pass.
   ********************************************************** */

void selectionSortAlg(int selectionSort[], const int NUMELS)
{
   int startScan = 0,
      minIndex = 0,
      minValue = 0,
      index = 0,
      swapCount = 0;

   bool swap = false;

   for (startScan = 0; startScan < NUMELS - 1; startScan++)
   {
      swap = false;

      minIndex = startScan;
      minValue = selectionSort[startScan];

      for (index = startScan + 1; index < NUMELS; index++)
      {
         if (selectionSort[index] < minValue)
         {
            minValue = selectionSort[index];
            minIndex = index;
            swap = true;
         }
      }

      cout << "\n\t\t";
      for (index = 0; index < NUMELS; index++)
      {
         cout << " " << selectionSort[index] << " ";
      }

      selectionSort[minIndex] = selectionSort[startScan];
      selectionSort[startScan] = minValue;
   }
}

Example Output:



No comments:

Post a Comment