fork download
  1. //Ryan Shahriyarpour CS1A Chapter 8 Question 6
  2. /*****************************************************************************
  3.  *
  4.  * String Selection Sort
  5.  *
  6.  *********************
  7.  *
  8.  * This program uses a selection sort, changed to work on strings instead
  9.  * of ints, to put a list of 20 names in alphabetical order. The names are
  10.  * displayed before and after the sort.
  11.  *
  12.  * *******************
  13.  * Selection sort: for each position in the array, find the smallest value
  14.  * in the part of the array that has not been sorted yet and swap it into
  15.  * that position. Strings are compared with < which orders them
  16.  * alphabetically.
  17.  *
  18.  * INPUT
  19.  * None (the names are stored in the program)
  20.  *
  21.  * OUTPUT
  22.  * The list of names before sorting and again after sorting
  23.  *
  24.  ****************************************************************************/
  25.  
  26. #include <iostream>
  27. #include <string>
  28. using namespace std;
  29.  
  30. // Constant for the number of names
  31. const int NUM_NAMES = 20;
  32.  
  33. // Function prototype
  34. void selectionSort(string array[], int size);
  35.  
  36. int main()
  37. {
  38. // data dictionary
  39. string names[NUM_NAMES] = { // Names to be sorted
  40. "Collins, Bill", "Smith, Bart", "Allen, Jim", "Griffin, Jim",
  41. "Stamey, Marty", "Rose, Geri", "Taylor, Terri", "Johnson, Jill",
  42. "Allison, Jeff", "Looney, Joe", "Wolfe, Bill", "James, Jean",
  43. "Weaver, Jim", "Pore, Bob", "Rutherford, Greg", "Javens, Renee",
  44. "Harrison, Rose", "Setzer, Cathy", "Pike, Gordon", "Holland, Beth"
  45. };
  46. int index; // Loop counter for displaying the names
  47.  
  48. // Display the names before sorting
  49. cout << "Names before sorting:" << endl;
  50. for (index = 0; index < NUM_NAMES; index++)
  51. cout << " " << names[index] << endl;
  52.  
  53. // Sort the names
  54. selectionSort(names, NUM_NAMES);
  55.  
  56. // Display the names after sorting
  57. cout << "\nNames after sorting:" << endl;
  58. for (index = 0; index < NUM_NAMES; index++)
  59. cout << " " << names[index] << endl;
  60.  
  61. return 0;
  62. }
  63.  
  64. // Sorts an array of strings into alphabetical order
  65. void selectionSort(string array[], int size)
  66. {
  67. // data dictionary
  68. int startScan; // Position being filled on this pass
  69. int index; // Loop counter that scans for the smallest
  70. int minIndex; // Index of the smallest string found
  71. string minValue; // The smallest string found
  72.  
  73. for (startScan = 0; startScan < size - 1; startScan++)
  74. {
  75. minIndex = startScan;
  76. minValue = array[startScan];
  77. for (index = startScan + 1; index < size; index++)
  78. {
  79. if (array[index] < minValue)
  80. {
  81. minValue = array[index];
  82. minIndex = index;
  83. }
  84. }
  85. array[minIndex] = array[startScan];
  86. array[startScan] = minValue;
  87. }
  88. }
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
Names before sorting:
  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

Names after sorting:
  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