//Sam Partovi                      CS1A                     Chapter 8, P 488, #6
//
/*******************************************************************************
* SORT NAMES ALPHABETICALLY
* ____________________________________________________________
* This program sorts a given list of names by alphabetical order with a sorting
* algorithm, and displays the names in their respective order.
* ____________________________________________________________
*INPUT
* NUM_NAMES: Number of names
* names: Initialization list for names
*
*OUTPUT
* startScan: Tracks starting variable in scan
* minValue: Tracks smallest value found in scan
* minIndex: Tracks the index of smallest value found in scan

******************************************************************************/
#include <iostream>
#include <string>
using namespace std;

const int NUM_NAMES = 20;    //INPUT - Array size for name count

//Function prototype for selectionSort
void selectionSort(string array[], int size);

int main() {

//INPUT - Initialization list for 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" };

//Call selectionSort function to sort names
 selectionSort(names, NUM_NAMES);
 
//Output sorted names
 for(int i = 0; i < NUM_NAMES; i++) {
 	cout << names[i] << "\n";
 }

 return 0;
 }

// *****************************************************************************
// Function definition for selectionSort:                                      *
// This function uses a selection sort algorithm to sort names in an array by  *
// alphabetical order.                                                         *
//******************************************************************************
void selectionSort(string array[], int size) {
 int startScan;  //OUTPUT - Tracks starting variable in scan
 int minIndex;   //OUTPUT - Tracks the index of smallest value found in scan
 
//Initialize minimum value to 0
 string minValue = array[0];  //OUTPUT - Tracks smallest value found in scan
 
//Perform selection sort on array
 for(startScan = 0; startScan < (size - 1); startScan++) {
  minIndex = startScan;
  minValue = array[startScan];
  
   for(int index = startScan + 1; index < size; index++) {
    if (array[index] < minValue) {
    
	 minValue = array[index];
     minIndex = index;
    }
  }
  
//Perform swap on smallest value
  array[minIndex] = array[startScan];
  array[startScan] = minValue;
 }
}