fork download
  1. //Ryan Robateau CSC5 Chapter 8, p. 488. #6
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * SORT NAMES
  6.  * _____________________________________________________________________________
  7.  * This purpose of this program is to sort names in order. The program will call
  8.  * a fucntion that will sort the names of people in order. After the function
  9.  * has sorted the names, then the program will display the names in order that
  10.  * is aphbaletical.
  11.  * _____________________________________________________________________________
  12.  ******************************************************************************/
  13.  
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <string>
  17. using namespace std;
  18.  
  19. void selectionSort(string names[], int size);
  20.  
  21. int main()
  22. {
  23. const int NAMES = 20;
  24. string names[NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
  25. "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
  26. "Taylor, Terri", "Johnson, Jill",
  27. "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
  28. "James, Jean", "Weaver, Jim", "Pore, Bob",
  29. "Rutherford, Greg", "Javens, Renee", "Harrison, Rose",
  30. "Setzer, Cathy", "Pike, Gordon", "Holland, Beth" };
  31. selectionSort(names, NAMES);
  32.  
  33. for(int i = 0; i < NAMES; i++)
  34. cout << names[i] << endl;
  35.  
  36. }
  37. void selectionSort(string names[], int size)
  38. {
  39. int startScan;
  40. int minIndex;
  41. string minValue;
  42.  
  43. for(startScan = 0; startScan < (size - 1); startScan++)
  44. {
  45. minIndex = startScan;
  46. minValue = names[startScan];
  47. for(int index = startScan + 1; index < size; index++)
  48. {
  49. if(names[index] < minValue)
  50. {
  51. minValue = names[index];
  52. minIndex = index;
  53. }
  54. }
  55. names[minIndex] = names[startScan];
  56. names[startScan] = minValue;
  57.  
  58. }
  59. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
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