fork download
  1. //Sam Partovi CS1A Chapter 8, P 488, #6
  2. //
  3. /*******************************************************************************
  4. * SORT NAMES ALPHABETICALLY
  5. * ____________________________________________________________
  6. * This program sorts a given list of names by alphabetical order with a sorting
  7. * algorithm, and displays the names in their respective order.
  8. * ____________________________________________________________
  9. *INPUT
  10. * NUM_NAMES: Number of names
  11. * names: Initialization list for names
  12. *
  13. *OUTPUT
  14. * startScan: Tracks starting variable in scan
  15. * minValue: Tracks smallest value found in scan
  16. * minIndex: Tracks the index of smallest value found in scan
  17.  
  18. ******************************************************************************/
  19. #include <iostream>
  20. #include <string>
  21. using namespace std;
  22.  
  23. const int NUM_NAMES = 20; //INPUT - Array size for name count
  24.  
  25. //Function prototype for selectionSort
  26. void selectionSort(string array[], int size);
  27.  
  28. int main() {
  29.  
  30. //INPUT - Initialization list for names
  31. string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
  32. "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
  33. "Taylor, Terri", "Johnson, Jill",
  34. "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
  35. "James, Jean", "Weaver, Jim", "Pore, Bob",
  36. "Rutherford, Greg", "Javens, Renee",
  37. "Harrison, Rose", "Setzer, Cathy",
  38. "Pike, Gordon", "Holland, Beth" };
  39.  
  40. //Call selectionSort function to sort names
  41. selectionSort(names, NUM_NAMES);
  42.  
  43. //Output sorted names
  44. for(int i = 0; i < NUM_NAMES; i++) {
  45. cout << names[i] << "\n";
  46. }
  47.  
  48. return 0;
  49. }
  50.  
  51. // *****************************************************************************
  52. // Function definition for selectionSort: *
  53. // This function uses a selection sort algorithm to sort names in an array by *
  54. // alphabetical order. *
  55. //******************************************************************************
  56. void selectionSort(string array[], int size) {
  57. int startScan; //OUTPUT - Tracks starting variable in scan
  58. int minIndex; //OUTPUT - Tracks the index of smallest value found in scan
  59.  
  60. //Initialize minimum value to 0
  61. string minValue = array[0]; //OUTPUT - Tracks smallest value found in scan
  62.  
  63. //Perform selection sort on array
  64. for(startScan = 0; startScan < (size - 1); startScan++) {
  65. minIndex = startScan;
  66. minValue = array[startScan];
  67.  
  68. for(int index = startScan + 1; index < size; index++) {
  69. if (array[index] < minValue) {
  70.  
  71. minValue = array[index];
  72. minIndex = index;
  73. }
  74. }
  75.  
  76. //Perform swap on smallest value
  77. array[minIndex] = array[startScan];
  78. array[startScan] = minValue;
  79. }
  80. }
Success #stdin #stdout 0.01s 5288KB
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