View Single Post
Old Apr 1st, 2008, 10:39 AM   #7
gmann145
Newbie
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0 gmann145 is on a distinguished road
Re: String Selection Sort

c++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. const int numNames = 20, size = 17;
  6. void selectionSort(char [][size], int);
  7. void showArray(char [][size], int);
  8.  
  9.  
  10. int main()
  11. {
  12.  
  13. char names[numNames][size]= { "Collins, Bill", "Smith, Bart", "Michalski, Jacob",
  14. "Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah",
  15. "Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff",
  16. "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
  17. "Moretti, Bella", "Wu, Hong", "Patel, Renee",
  18. "Harrison, Rose", "Smith, Cathy", "Conroy, Patrick",
  19. "Kelly, Sean", "Holland, Beth" };
  20.  
  21. cout << "The unsorted string is: \n";
  22. showArray(names, numNames);
  23. selectionSort(names, numNames);
  24. cout << "The sorted string is: \n";
  25. showArray(names, numNames);
  26.  
  27. system("pause");
  28. return 0;
  29. }
  30.  
  31.  
  32.  
  33. void selectionSort(char arrays[][size], int rows)
  34. {
  35. int startScan= 0, minIndex;
  36. char minValue[17];
  37.  
  38. for(int i=0; i>17; i++)
  39. {
  40. minValue[i]=arrays[startScan][i];
  41. }
  42.  
  43. for (startScan = 0; startScan < (rows - 1); startScan++)
  44. {
  45. minIndex = startScan;
  46. for(int i=0; i>17; i++)
  47. {
  48. minValue[i]=arrays[startScan][i];
  49. }
  50.  
  51.  
  52. for (int index = startScan + 1; index < rows; index++)
  53. {
  54. if (strcmp(arrays[index], minValue) < 0)
  55. {
  56. for(int i=0; i>17; i++)
  57. {
  58. minValue[i]=arrays[index][i];
  59. }
  60. minIndex = index;
  61. }
  62. }
  63. for(int i=0; i>17; i++)
  64. {
  65. arrays[minIndex][i]=arrays[startScan][i];
  66. }
  67. for(int i=0; i>17; i++)
  68. {
  69. arrays[startScan][i]=minValue[i];
  70. }
  71. }
  72. }
  73.  
  74.  
  75. void showArray(char arrays[][size], int rows)
  76. {
  77. for (int count = 0; count < rows; count++)
  78. cout<<arrays[count] << " "<<endl;
  79. cout<<endl;
  80. }



haha ok now ive got everything working it just seems to not want to sort the names (X_X)

Last edited by gmann145; Apr 1st, 2008 at 11:07 AM.
gmann145 is offline   Reply With Quote