Tuesday, March 7, 2017

Programming Challenge 8.6 - String Selection Sort

/* String Selection Sort - This program uses a modified version of the
   selection sort algorithm presented in chapter 8, so that it sorts
   an array of strings instead of an array of integers. */

#include "Utility.h"

void selectionSort(string[], int);
void displayUnsortedList(const string[], int);
void displaySortedList(const string[], int);


int main()
{
   const int NUM_NAMES = 20;

   /* Array holding first and last names */
   string names[NUM_NAMES] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",
                               "Griffin Jim", "Stamey, Marty", "Rose, Geri",
                               "Taylor, Terri", "Johnson, Jill", "Allison, Jeff",
                               "Looney, Joe", "Wolfe, Bill", "James, Jean",
                               "Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
                               "Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
                               "Pike, Gordon", "Holland, Beth" };

   /* Displays the vanilla name list */
   displayUnsortedList(names, NUM_NAMES);

   /* Sorts the list in alphabetic order */
   selectionSort(names, NUM_NAMES);

   /* Displays the name list in alphabetical order */
   displaySortedList(names, NUM_NAMES);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: selectionSort

   This function accepts names and NUM_Names, containing the
   number of elements stored in the array, as its arguments.
   It uses election sort algorithm is used to sort the names
   in alphabetical order.
   ********************************************************** */

void selectionSort(string names[], int NUM_NAMES)
{
   int startScan = 0,
       minIndex = 0,
       index = 0;

   string minAlpha = " ";

   for (startScan = 0; startScan < (NUM_NAMES - 1); startScan++)
   {
      minIndex = startScan;
      minAlpha = names[startScan];

      for (index = startScan + 1; index < NUM_NAMES; index++)
      {

         if (names[index] < minAlpha)
         {
            minAlpha = names[index];
            minIndex = index;
         }
      }

      names[minIndex] = names[startScan];
      names[startScan] = minAlpha;
   }         
}

/* **********************************************************
   Definition: displayUnsortedList

   This function accepts names and NUM_Names, containing the
   number of elements stored in the array, as its arguments.
   It displays the list of names in unsorted order.
   ********************************************************** */

void displayUnsortedList(const string names[], int NUM_NAMES)
{
   cout << "\n\t\tName Database - Unsorted List:\n\n";
  
   for (int index = 0; index < NUM_NAMES; index++)
   {
      cout << "\t\t" << names[index] << " \n";
   }
   cout << "\t\t";
}

/* **********************************************************
   Definition: displaySortedList

   This function accepts names and NUM_Names, containing the
   number of elements stored in the array, as its arguments.
   It displays the list of names in alphabetical order.
   ********************************************************** */

void displaySortedList(const string names[], int NUM_NAMES)
{
   cout << "\n\t\tName Database - Sorted List:\n\n";

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

Example Output:


No comments:

Post a Comment