I've implemented a class (kind of) to deal with the BubbleSort. I have a few problems which I need addressed.
Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//String sort class
class stringarray
{
private:
string temp; //used by swap
string lhs; //used by swap
string rhs; //used by swap
public:
stringarray(){temp = "";lhs = "";rhs = "";} //Constructor
~stringarray() {} //Destructor
void bubbleSort(string* array, const size_t size); //Sort method
void swap(string& lhs, string& rhs){ temp=lhs; lhs = rhs; rhs = temp; } //Swap method
void printArray(string* array, const size_t size); //Print method
}sortthem;
//Main
void main()
{
string terminate = "q";
int i;//Iterator
//Declare initial string size
string RandomSize[50];
const size_t arrSize = sizeof RandomSize/ sizeof(*RandomSize);
cout <<"Enter string or press 'q' to quit" << endl;
//Prompt for input until user quits
for (i=0;i<50;i++)
{
cout <<"String" << i+1 << ": ";
cin >> RandomSize[i];
if (RandomSize[i].length() == 1 && RandomSize[i].compare(terminate))
{
break;
system("pause");
}
}
//Sort array and Print to screen
cout << endl << "Bubble Sorted:" << endl;
sortthem.bubbleSort(RandomSize, arrSize);
sortthem.printArray(RandomSize, arrSize);
}
//BubbleSort
void stringarray::bubbleSort(string* array, const size_t size)
{
for(int i = size; i>=0; --i)
{
for(int j= 0 ; j<size-1 ; j++)
{
if (array[j] > array[j + 1])
swap(array[j], array[j + 1]);
}
}
}
//PrintArray
void stringarray::printArray(string* array, const size_t size)
{
for (int i = 0; i < size; i++)
{
while(array[i] == "")
i++;
cout <<array[i] << endl;
}
} The things I could not figure out were:
1) I have declared an array of 50 strings, inputting until Q is reached (only capital right now). After I hit Q to terminate the strings, it sorts Q as if it were an inputted string.
I will get started on the sort by ascending or descending as soon as I get a chance. My lab class is over...