View Single Post
Old Mar 25th, 2008, 10:31 AM   #1
gmann145
Newbie
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0 gmann145 is on a distinguished road
Exclamation String Selection Sort

hey all,, i have to use a selection sort function to search an array of strings, this is what ive got so far, im not sure where to put the selectionSort in the main and so still unsure if its correct



c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void selectionSort(char [], int);
  5.  
  6.  
  7. int main()
  8. {
  9. const int numNames = 20, size = 17;
  10. char names[numNames][size]={"Collins, Bill", "Smith, Bart", "Allen, Jim",
  11. "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
  12. "Taylor, Terri", "Looney, Joe", "Wolfe, Bill",
  13. "James, Jean", "Weaver, Jim", "Pore, Bob",
  14. "Mann, Gregg", "Schneider, Jon", "Johnson, Jill",
  15. "Harrison, Rose", "Setzer, Cathy",
  16. "Pike, Gordon", "Holland, Beth"};
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. system("pause");
  24. return 0;
  25. }
  26.  
  27.  
  28. void selectionSort(char arrays[][SIZE], int rows)
  29. {
  30. int startScan, minIndex, index;
  31. int z;
  32. char minValue[NUM_NAMES][size];
  33.  
  34. for (startScan = 0; startScan < (rows - 1); startScan++)
  35. {
  36. z = startScan;
  37. strncpy(minValue[0], arrays[startScan], 20);
  38.  
  39. for (index = startScan + 1; index < rows; index++)
  40. {
  41. if (strcmp(arrays[index], minValue[NUM_NAMES]) < 0)
  42. {
  43. strncpy(minValue[0], arrays[index], 20);
  44. minIndex = arrays[z];
  45. }
  46. }
  47. strncpy(arrays[startScan], arrays[minIndex] , 20);
  48. }
  49. }
gmann145 is offline   Reply With Quote