//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;
}
}
Ly9TYW0gUGFydG92aSAgICAgICAgICAgICAgICAgICAgICBDUzFBICAgICAgICAgICAgICAgICAgICAgQ2hhcHRlciA4LCBQIDQ4OCwgIzYKLy8KLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKKiBTT1JUIE5BTUVTIEFMUEhBQkVUSUNBTExZCiogX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiogVGhpcyBwcm9ncmFtIHNvcnRzIGEgZ2l2ZW4gbGlzdCBvZiBuYW1lcyBieSBhbHBoYWJldGljYWwgb3JkZXIgd2l0aCBhIHNvcnRpbmcKKiBhbGdvcml0aG0sIGFuZCBkaXNwbGF5cyB0aGUgbmFtZXMgaW4gdGhlaXIgcmVzcGVjdGl2ZSBvcmRlci4KKiBfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KKklOUFVUCiogTlVNX05BTUVTOiBOdW1iZXIgb2YgbmFtZXMKKiBuYW1lczogSW5pdGlhbGl6YXRpb24gbGlzdCBmb3IgbmFtZXMKKgoqT1VUUFVUCiogc3RhcnRTY2FuOiBUcmFja3Mgc3RhcnRpbmcgdmFyaWFibGUgaW4gc2NhbgoqIG1pblZhbHVlOiBUcmFja3Mgc21hbGxlc3QgdmFsdWUgZm91bmQgaW4gc2NhbgoqIG1pbkluZGV4OiBUcmFja3MgdGhlIGluZGV4IG9mIHNtYWxsZXN0IHZhbHVlIGZvdW5kIGluIHNjYW4KCioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29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IG5hbWVzCiBmb3IoaW50IGkgPSAwOyBpIDwgTlVNX05BTUVTOyBpKyspIHsKIAljb3V0IDw8IG5hbWVzW2ldIDw8ICJcbiI7CiB9CgogcmV0dXJuIDA7CiB9CgovLyAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgovLyBGdW5jdGlvbiBkZWZpbml0aW9uIGZvciBzZWxlY3Rpb25Tb3J0OiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKgovLyBUaGlzIGZ1bmN0aW9uIHVzZXMgYSBzZWxlY3Rpb24gc29ydCBhbGdvcml0aG0gdG8gc29ydCBuYW1lcyBpbiBhbiBhcnJheSBieSAgKgovLyBhbHBoYWJldGljYWwgb3JkZXIuICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKgovLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgp2b2lkIHNlbGVjdGlvblNvcnQoc3RyaW5nIGFycmF5W10sIGludCBzaXplKSB7CiBpbnQgc3RhcnRTY2FuOyAgLy9PVVRQVVQgLSBUcmFja3Mgc3RhcnRpbmcgdmFyaWFibGUgaW4gc2NhbgogaW50IG1pbkluZGV4OyAgIC8vT1VUUFVUIC0gVHJhY2tzIHRoZSBpbmRleCBvZiBzbWFsbGVzdCB2YWx1ZSBmb3VuZCBpbiBzY2FuCiAKLy9Jbml0aWFsaXplIG1pbmltdW0gdmFsdWUgdG8gMAogc3RyaW5nIG1pblZhbHVlID0gYXJyYXlbMF07ICAvL09VVFBVVCAtIFRyYWNrcyBzbWFsbGVzdCB2YWx1ZSBmb3VuZCBpbiBzY2FuCiAKLy9QZXJmb3JtIHNlbGVjdGlvbiBzb3J0IG9uIGFycmF5CiBmb3Ioc3RhcnRTY2FuID0gMDsgc3RhcnRTY2FuIDwgKHNpemUgLSAxKTsgc3RhcnRTY2FuKyspIHsKICBtaW5JbmRleCA9IHN0YXJ0U2NhbjsKICBtaW5WYWx1ZSA9IGFycmF5W3N0YXJ0U2Nhbl07CiAgCiAgIGZvcihpbnQgaW5kZXggPSBzdGFydFNjYW4gKyAxOyBpbmRleCA8IHNpemU7IGluZGV4KyspIHsKICAgIGlmIChhcnJheVtpbmRleF0gPCBtaW5WYWx1ZSkgewogICAgCgkgbWluVmFsdWUgPSBhcnJheVtpbmRleF07CiAgICAgbWluSW5kZXggPSBpbmRleDsKICAgIH0KICB9CiAgCi8vUGVyZm9ybSBzd2FwIG9uIHNtYWxsZXN0IHZhbHVlCiAgYXJyYXlbbWluSW5kZXhdID0gYXJyYXlbc3RhcnRTY2FuXTsKICBhcnJheVtzdGFydFNjYW5dID0gbWluVmFsdWU7CiB9Cn0=
stdout
Allen, Jim
Allison, Jeff
Collins, Bill
Griffin, Jim
Harrison, Rose
Holland, Beth
James, Jean
Javens, Renee
Johnson, Jill
Looney, Joe
Pike, Gordon
Pore, Bob
Rose, Geri
Rutherford, Greg
Setzer, Cathy
Smith, Bart
Stamey, Marty
Taylor, Terri
Weaver, Jim
Wolfe, Bill