Jump to content

C++ bubble swap string, please help!

Helepolis

So I'm in my first C++ course and we were given an assignment to convert a int bubble sort program to one that accepts and works with strings

 

I've gotten it to accept strings and print them but honestly have no idea how to make the bubble sort function.

 

This is what I currently have 

 

#include<iostream>
#include<vector>
#include<cstdlib>
 
using namespace std;
 
void getInts(vector<string>&);
void displayInts(vector<string>);
void bubbleSort(string floatArr[], int size);
 
int main() 
{
  vector<string> names;
  getInts(names);
  displayInts(names);
  system("pause");
  return EXIT_SUCCESS;
}
 
void getInts(vector<string>& names) 
{
string tmp;
 
  while(true) {
cout << "Enter a name (quit to stop): ";
  cin >> tmp;
  if (tmp == "quit") break;
  names.push_back(tmp);
 
  }
}
 
void displayInts(vector<string> names)
{
  for(int i = 0; i < names.size(); i++) {
    cout << names << endl;
    bubbleSort(i , size);
  }
}
 
void bubbleSort(string floatArr[], int size) 
  for (string i = 1; i < size; i++)
    // bubble up max {floatArr[0..size-i]}:
    for (string j = 0; j < size - i; j++)
      if (floatArr[j] > floatArr[j+1])
        swap(floatArr[j], floatArr[j+1]);
}
 
 
I would be very grateful is someone can point me in the right direction.
Link to comment
Share on other sites

Link to post
Share on other sites

Looks like you have the basics down. Below is just code to sort ints.

 

Remember that chars are just 8 bits or you can just cast the char to an int. It will do the same thing.

void BubbleSort( int lengthOfSort ) {    bool swapping = true;    while( swapping ) {        swapping = false;        for( int i = 0; i < lengthOfSort - 1; i++ ) {            if ( array[i] > array[i+1] ) {                swap( &array[i], &array[i+1] );                swapping = true;            }        }    }}
Link to comment
Share on other sites

Link to post
Share on other sites

Just treat the strings as arrays of chars

bool compare(const string& first, const string& second) //eqiv to first>second{  int i=0;  while ( (i<first.length()) && (i<second.length()) )  {    if (tolower(first[i])>tolower(second[i])) return true;    else if (tolower(first[i])<tolower(second[i])) return false;    ++i;  }  return ( first.length() < second.length() ); //we ran out of chars and they are the same so far so return whichever one is longer}

Replace the comparison in the above bubbleSort code to compare(first,second) and you will be up and running.

 

Cheers.

Link to comment
Share on other sites

Link to post
Share on other sites

The answer's already posted, but there are two bugs in your bubbleSort method.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×