1 Replies - 4818 Views - Last Post: 24 November 2008 - 03:14 AM Rate Topic: -----

#1 wany23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-November 08

Selection Sort Vector of Strings

Posted 24 November 2008 - 02:50 AM

I have to write a program that will sort a string of vectors by alphabetical order:
for example
"How are you"
will be
Haeooruwy

here is what I have so far
however it does not seem to sort the string but rather output it exactly what was inputted:
for example when i input "Hey guys" it will output Hey guys but that is not what i want it to output

thanks in advance

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>

using namespace std;

void selSort(vector<string>& v);

int main()

{
	int answer;
	vector<string> v;
	string next;
 

   getline(cin, next);
		  
		  v.push_back(next);

		  
		  

	  selSort(v);
	  for (int i= 0; i < v.size(); i++)
		cout << v[i] << ' ';
	  cout << endl;
	
		  
		  getchar ();
		  return 0;
}

void selSort(vector<string>& v)
{
	for (int pass=0; pass<v.size()-1; pass++) {
		int potentialSmallest = pass;  


		for (int i=pass+1; i<v.size(); i++) {
			if (v[i] < v[potentialSmallest]) {
			  
				potentialSmallest = i;
			}
		}
		
	 
		string temp = v[pass];
		v[pass] = v[potentialSmallest];
		v[potentialSmallest] = temp;

		}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Selection Sort Vector of Strings

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Selection Sort Vector of Strings

Posted 24 November 2008 - 03:14 AM

Just a gentle and friendly tip.
It would make it easier to help if you cleaned out some of the redundant lines and code before you posted it.

Anyway, have a look at the "// DEBUG ..." lines I have added to your code.
What do you expect them to produce?
If you compile and run what do they produce?
Does that give you a hint on where to look for bugs?

#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>

using namespace std;

void selSort(vector<string>& v);

int main()
{
    int answer;
    vector<string> v;
   vector<string> v2;
    string next;

   getline(cin, next);
   v.push_back(next);

      selSort(v);
      for (int i= 0; i < v.size(); i++)
        cout << v[i] << ' ';
      cout << endl;
          //getchar ();
          return 0;
}

void selSort(vector<string>& v)
{
// DEBUG output - delete this
cout << "Entered selSort" << endl;
	for (int pass=0; pass<v.size()-1; pass++) {
        int potentialSmallest = pass;  
// DEBUG output - delete this
cout << "Entered outerForLoop" << endl;
        for (int i=pass+1; i<v.size(); i++) {
// DEBUG output - delete this
cout << "Entered innerForLoop" << endl;
            if (v[i] < v[potentialSmallest]) {
                potentialSmallest = i;
            }
        }
        string temp = v[pass];
        v[pass] = v[potentialSmallest];
        v[potentialSmallest] = temp;
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1