|
Newbie
Join Date: Apr 2008
Posts: 10
Rep Power: 0 
|
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:
#include <iostream> #include <vector> using namespace std; int main() { vector<string> v(0); v.reserve(50); const int numNames = 20, size = 17; char names[numNames][size]= { "Collins, Bill", "Smith, Bart", "Michalski, Jacob", "Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah", "Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff", "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean", "Moretti, Bella", "Wu, Hong", "Patel, Renee", "Harrison, Rose", "Smith, Cathy", "Conroy, Patrick", "Kelly, Sean", "Holland, Beth" }; for(int i=0;i<numNames;i++) v.push_back(names[i]); //insertion sort /* for(int i=1; i<v.size(); i++) { string tmp = v[i]; int j; for(j = i; j > 0 && v[j-1].compare(tmp) > 0 ;j--) v[j] = v[j-1]; v[j]=tmp; } */ //selection sort for(int i=0;i<v.size()-1;i++) for(int j=i+1;j<v.size();j++) if(v[j].compare(v[i]) < 0) v[j].swap(v[i]); for(int i=0;i<v.size();i++) cout<<v[i]<<endl; }
Last edited by misho; Apr 19th, 2008 at 9:55 PM.
|