fork download
  1. //Maxwell Brewer CS1A Chapter 8, p. 488, #6
  2. //
  3. /*******************************************************************************
  4.  * STRING SELECTION SORT
  5.  * _____________________________________________________________________________
  6.  * This program will sort a list of names in ascending order using selection
  7.  * sort and display the sorted list.
  8.  * _____________________________________________________________________________
  9.  * INPUT
  10.  * None (The list of names is predefined).
  11.  *
  12.  * OUTPUT
  13.  * The sorted list of names in ascending order.
  14.  *******************************************************************************/
  15.  
  16. #include <iostream>
  17. #include <string>
  18. using namespace std;
  19.  
  20. // Function prototypes
  21.  
  22. // This function will sort the array of names in ascending order
  23. void stringSort(string [], int);
  24.  
  25. // This function will display the sorted array of names
  26. void displayData(const string [], int);
  27.  
  28. int main() {
  29. const int NUM_NAMES = 21;
  30. string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
  31. "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
  32. "Taylor, Terri", "Johnson, Jill",
  33. "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
  34. "James, Jean", "Weaver, Jim", "Pore, Bob",
  35. "Rutherford, Greg", "Javens, Renee",
  36. "Harrison, Rose", "Setzer, Cathy",
  37. "Pike, Gordon", "Holland, Beth",
  38. "Argila, Carl"};
  39.  
  40. // Call sorting function
  41. stringSort(names, NUM_NAMES);
  42.  
  43. // Display sorted data
  44. cout << "Sorted list of names:\n";
  45. displayData(names, NUM_NAMES);
  46.  
  47. return 0;
  48. }
  49.  
  50. void stringSort(string names[], int arraySize) {
  51. // Sort the names array in ascending order using selection sort
  52. int minIndex;
  53. string minName;
  54.  
  55. // Outer loop iterates over each element
  56. // in the array (except the last) as the starting point
  57. for(int startScan = 0; startScan < arraySize - 1; startScan++) {
  58.  
  59. // Assume the first unsorted element is the smallest
  60. minName = names[startScan];
  61. minIndex = startScan;
  62.  
  63. // Inner loop searches the rest of the array for the smallest element
  64. for(int index = startScan + 1; index < arraySize; index++) {
  65. if(names[index] < minName) {
  66. minName = names[index]; // Update the smallest element found
  67. minIndex = index;
  68. }
  69. }
  70.  
  71. // Swap the smallest element found with
  72. //the element at startScan position
  73. names[minIndex] = names[startScan];
  74. names[startScan] = minName;
  75. }
  76. }
  77.  
  78. void displayData(const string names[], int arraySize) {
  79. // Display the sorted list of names
  80. for(int counter = 0; counter < arraySize; counter++) {
  81. cout << names[counter] << endl;
  82. }
  83. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Sorted list of names:
Allen, Jim
Allison, Jeff
Argila, Carl
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