View Single Post
Old Apr 19th, 2008, 9:34 PM   #11
misho
Newbie
 
Join Date: Apr 2008
Posts: 10
Rep Power: 0 misho is on a distinguished road
Re: String Selection Sort

I have an exam on this stuff (about 1/3 of it is various sorting techniques) in 3 days . Here's one of the practice programs I made (I threw your list of names at the beginning, so it's a bit more relevant to you, but the rest of it uses vectors and strings, so I'm not sure it's what you want). Maybe it'll be of some help:

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main()
  5. {
  6. vector<string> v(0);
  7. v.reserve(50);
  8.  
  9. const int numNames = 20, size = 17;
  10. char names[numNames][size]= {
  11. "Collins, Bill", "Smith, Bart",
  12. "Michalski, Jacob", "Griffin, Jim",
  13. "Sanchez, Manny", "Rubin, Sarah",
  14. "Taylor, Tyrone", "Johnson, Jill",
  15. "Allison, Jeff", "Moreno, Juan",
  16. "Wolfe, Bill", "Whitman, Jean",
  17. "Moretti, Bella", "Wu, Hong",
  18. "Patel, Renee", "Harrison, Rose",
  19. "Smith, Cathy", "Conroy, Patrick",
  20. "Kelly, Sean", "Holland, Beth" };
  21.  
  22. for(int i=0;i<numNames;i++) v.push_back(names[i]);
  23.  
  24. //insertion sort
  25. /* for(int i=1; i<v.size(); i++)
  26. {
  27. string tmp = v[i];
  28. int j;
  29. for(j = i; j > 0 && v[j-1].compare(tmp) > 0 ;j--)
  30. v[j] = v[j-1];
  31. v[j]=tmp;
  32. }
  33. */
  34. //selection sort
  35. for(int i=0;i<v.size()-1;i++)
  36. for(int j=i+1;j<v.size();j++)
  37. if(v[j].compare(v[i]) < 0) v[j].swap(v[i]);
  38.  
  39. for(int i=0;i<v.size();i++) cout<<v[i]<<endl;
  40. }

Last edited by misho; Apr 19th, 2008 at 9:55 PM.
misho is offline   Reply With Quote