//Maxwell Brewer CS1A Chapter 8, p. 488, #6
//
/*******************************************************************************
* STRING SELECTION SORT
* _____________________________________________________________________________
* This program will sort a list of names in ascending order using selection
* sort and display the sorted list.
* _____________________________________________________________________________
* INPUT
* None (The list of names is predefined).
*
* OUTPUT
* The sorted list of names in ascending order.
*******************************************************************************/
#include <iostream>
#include <string>
using namespace std;
// Function prototypes
// This function will sort the array of names in ascending order
void stringSort( string [ ] , int ) ;
// This function will display the sorted array of names
void displayData( const string [ ] , int ) ;
int main( ) {
const int NUM_NAMES = 21 ;
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" ,
"Argila, Carl" } ;
// Call sorting function
stringSort( names, NUM_NAMES) ;
// Display sorted data
cout << "Sorted list of names:\n " ;
displayData( names, NUM_NAMES) ;
return 0 ;
}
void stringSort( string names[ ] , int arraySize) {
// Sort the names array in ascending order using selection sort
int minIndex;
string minName;
// Outer loop iterates over each element
// in the array (except the last) as the starting point
for ( int startScan = 0 ; startScan < arraySize - 1 ; startScan++ ) {
// Assume the first unsorted element is the smallest
minName = names[ startScan] ;
minIndex = startScan;
// Inner loop searches the rest of the array for the smallest element
for ( int index = startScan + 1 ; index < arraySize; index++ ) {
if ( names[ index] < minName) {
minName = names[ index] ; // Update the smallest element found
minIndex = index;
}
}
// Swap the smallest element found with
//the element at startScan position
names[ minIndex] = names[ startScan] ;
names[ startScan] = minName;
}
}
void displayData( const string names[ ] , int arraySize) {
// Display the sorted list of names
for ( int counter = 0 ; counter < arraySize; counter++ ) {
cout << names[ counter] << endl;
}
}
Ly9NYXh3ZWxsIEJyZXdlciAgICAgICAgICAgICAgICAgQ1MxQSAgICAgICAgICAgICAgICAgICAgIENoYXB0ZXIgOCwgcC4gNDg4LCAgIzYKLy8KLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogU1RSSU5HIFNFTEVDVElPTiBTT1JUCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIFRoaXMgcHJvZ3JhbSB3aWxsIHNvcnQgYSBsaXN0IG9mIG5hbWVzIGluIGFzY2VuZGluZyBvcmRlciB1c2luZyBzZWxlY3Rpb24KICogc29ydCBhbmQgZGlzcGxheSB0aGUgc29ydGVkIGxpc3QuCiAqIF9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fCiAqIElOUFVUCiAqICAgTm9uZSAoVGhlIGxpc3Qgb2YgbmFtZXMgaXMgcHJlZGVmaW5lZCkuCiAqIAogKiBPVVRQVVQKICogICBUaGUgc29ydGVkIGxpc3Qgb2YgbmFtZXMgaW4gYXNjZW5kaW5nIG9yZGVyLgogKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKi8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIEZ1bmN0aW9uIHByb3RvdHlwZXMKCi8vIFRoaXMgZnVuY3Rpb24gd2lsbCBzb3J0IHRoZSBhcnJheSBvZiBuYW1lcyBpbiBhc2NlbmRpbmcgb3JkZXIKdm9pZCBzdHJpbmdTb3J0KHN0cmluZyBbXSwgaW50KTsgCgovLyBUaGlzIGZ1bmN0aW9uIHdpbGwgZGlzcGxheSB0aGUgc29ydGVkIGFycmF5IG9mIG5hbWVzCnZvaWQgZGlzcGxheURhdGEoY29uc3Qgc3RyaW5nIFtdLCBpbnQpOyAKCmludCBtYWluKCkgewogICAgY29uc3QgaW50IE5VTV9OQU1FUyA9IDIxOwogICAgc3RyaW5nIG5hbWVzW05VTV9OQU1FU10gPSB7IkNvbGxpbnMsIEJpbGwiLCAiU21pdGgsIEJhcnQiLCAiQWxsZW4sIEppbSIsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIkdyaWZmaW4sIEppbSIsICJTdGFtZXksIE1hcnR5IiwgIlJvc2UsIEdlcmkiLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICJUYXlsb3IsIFRlcnJpIiwgIkpvaG5zb24sIEppbGwiLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICJBbGxpc29uLCBKZWZmIiwgIkxvb25leSwgSm9lIiwgIldvbGZlLCBCaWxsIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAiSmFtZXMsIEplYW4iLCAiV2VhdmVyLCBKaW0iLCAiUG9yZSwgQm9iIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAiUnV0aGVyZm9yZCwgR3JlZyIsICJKYXZlbnMsIFJlbmVlIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAiSGFycmlzb24sIFJvc2UiLCAiU2V0emVyLCBDYXRoeSIsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIlBpa2UsIEdvcmRvbiIsICJIb2xsYW5kLCBCZXRoIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAiQXJnaWxhLCBDYXJsIn07CgogICAgLy8gQ2FsbCBzb3J0aW5nIGZ1bmN0aW9uCiAgICBzdHJpbmdTb3J0KG5hbWVzLCBOVU1fTkFNRVMpOwoKICAgIC8vIERpc3BsYXkgc29ydGVkIGRhdGEKICAgIGNvdXQgPDwgIlNvcnRlZCBsaXN0IG9mIG5hbWVzOlx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