My goal is to sort a list of words according to two criteria:
- the primary criterion is increasing word length,
- the secondary criterion is alphabetical order
For example:
{red,a,three,b,four,aeoli}
should be sorted as:
{a,b,red,four,aeoli,three}.
I have two separate quicksorts: one on length followed by one on alphabetical.
I was just curious how to merge these two? The main problem I am encountering is I don't know how to sort alphabetically while taking the length into mind, and running through the whole list.
Any advice is appreciated, the following code is my main and the quicksort functions:
Vector<String> words;
String word;
ifstream inFile;
inFile.open("practice.txt");
while(!inFile.eof()){
getLine(inFile,word);
words.push_back(word);
}
inFile.close();
String pivot = "qog";
if(words[2] < pivot)
cout << "Bigger" << endl;
words.quicksort(2,words.length()-2);
words.quicksort2(2,words.length()-2);
the quicksort for length
template <typename T>
void Vector<T>::quicksort(int left, int right)
{
int i = left;
int j = right;
String pivot = data[(left+right)/2];
if(i <= j)
{
while(data[i].getLength() < pivot.getLength())
i++;
while(pivot.getLength() < data[j].getLength())
j--;
}
if(i <= j)
{
String temp = data[i];
data[i] = data[j];
data[j] = temp;
i++;
j--;
}
if(left < j)
quicksort(left,j);
if(i < right)
quicksort(i,right);
}
and the one for letters
template <typename T>
void Vector<T>::quicksort2(int left, int right)
{
int i = left;
int j = right;
String pivot = data[(left+right)/2];
if(i <= j)
{
while(data[i] < pivot)
i++;
while(pivot < data[j])
j--;
}
if(i <= j)
{
String temp = data[i];
data[i] = data[j];
data[j] = temp;
i++;
j--;
}
if(left < j)
quicksort2(left,j);
if(i < right)
quicksort2(i,right);
}
NOTE: This is done with my own custom Vector and String classes, both versions run perfectly, so my main problem is the logic of the sort itself -- how to get them to run together.
Also I can only use iostream, fstream and cstring, along with anything else I implement myself.
Thanks in advance.