Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 1:01 PM
Throwing an exception when using string constructor csrocker101 C# 3 Apr 8th, 2007 2:04 PM
Help with breaking apart a string csrocker101 C# 6 Apr 6th, 2007 7:50 AM
Function Parameters grimpirate PHP 10 Mar 14th, 2007 6:55 PM
selection sort hopeolicious C++ 2 Mar 15th, 2005 12:47 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC