fork download
  1. //Nathan Dominguez CSC5 Chapter 8, P.488, #6
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Modify Selection Sort
  6.  * _____________________________________________________________________________
  7.  * This program sorts 20 names and displays them before and after they are
  8.  * sorted. They are sorted by a selection array.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  *
  12.  * OUTPUT
  13.  * names : The names in the array
  14.  *****************************************************************************/
  15. #include <iostream>
  16. #include <iomanip>
  17. #include <string>
  18. using namespace std;
  19.  
  20. const int NUM_NAMES = 20; //amount of names in the array
  21. void selectionSort(string[], int); //sorts names
  22. void Display(const string[], int); //displays names
  23. int main() {
  24. //
  25. //initalized array with 20 names
  26. string names[NUM_NAMES] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",
  27. "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",
  28. "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
  29. "James, Jean", "Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
  30. "Javens, Renee", "Harrison, Rose", "Setzer, Cathy", "Pike, Gordon",
  31. "Holland, Beth" };
  32. //
  33. //calls functions Display and selectionSort
  34. Display(names, NUM_NAMES);
  35. selectionSort(names, NUM_NAMES);
  36. Display(names, NUM_NAMES);
  37.  
  38. return 0;
  39. }
  40. //
  41. //sorts the names with selection sort
  42. void selectionSort(string array[], int NUM_NAMES) {
  43. int start;
  44. int mIndex;
  45. string MinV;
  46.  
  47. for (start = 0; start < (NUM_NAMES - 1); start++) {
  48. mIndex = start;
  49. MinV = array[start];
  50. for (int index = start + 1; index < NUM_NAMES; index++) {
  51. if (array[index] < MinV) {
  52. MinV = array[index];
  53. mIndex = index;
  54. }
  55. }
  56. array[mIndex] = array[start];
  57. array[start] = MinV;
  58. }
  59. }
  60. //
  61. //displays the names
  62. void Display(const string array[], int size) {
  63. for (int count = 0; count < size; count++) {
  64. cout << array[count] << endl;
  65. }
  66. cout << endl << endl << endl << endl << endl << endl;
  67. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
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






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