I am having issues implementing a quicksort to sort an array of strings. I am also relatively new to c++ so still struggling with some issues there. Right now my code correctly reads in and creates an array of strings but I run into problems when I try to use my quicksort algorithm. The first problem I am running into is that I believe that the recursion is not stopping when it should. I get a segmentation fault after the quicksort runs for a little bit.
Code (Modified):
#include "MyParser.h"
#include <iostream>
#include <fstream>
#include <string>
void resize(string*& words, int size)
{
string* newArray = new string[size*2];
for (int i = 0; (i < size)&&(i<size*2); i++)
newArray[i] = words[i];
for (int i = size; i < size*2; i++)
newArray[i] = "";
delete[] words;
words = newArray;
}
void partitionArray(string*& words, int& left, int& right, int pi)
{
int i = left;
int j = right;
string tmp;
string pivot = words[pi];
while (i < j) {
string str1 = words[i];
string str2 = words[j];
while ((str1.compare(pivot) < 0) && (i < right))
i++;
while ((str2.compare(pivot) >= 0) && (j > left))
j--;
if (i <= j)
{
tmp = words[i];
words[i] = words[j];
words[j] = tmp;
i++;
j--;
}
};
}
void quickSort(string*& words, int left, int right)
{
int i = left;
int j = right;
string tmp;
string pivot = words[(left + right) / 2];
/* partition */
int pivotIndex = (left + right) / 2;
pivotIndex = partitionArray(words, 0, right, pivotIndex);
cout << "start recursion" << endl;
/* recursion */
if (left < j)
quickSort(words, left, j);
if (i < right)
quickSort(words, i, right);
}
int main()
{
// define file reader
ofstream outData;
outData.open("logData.txt");
Parser* myParser = new Parser("testData.txt");
int sizeOfArray = 500;
string* words = new string[sizeOfArray];
int index = 0;
while(myParser->hasTokens())
{
if (index >= sizeOfArray)
{
resize(words, sizeOfArray);
sizeOfArray = sizeOfArray*2;
}
string currentWord = myParser->nextToken();
if (currentWord != "")
{
words[index] = currentWord;
index++;
}
}
int lastWordInArrayIndex = index;
quickSort(words, 0, lastWordInArrayIndex);
return 0;
}
Any help on this would be greatly appreciated!
MODIFIED
right now it will sort the following 11 elements correctly:
adfgh
btyui
dfghj
eerty
fqwre
kyuio
verty
wwert
yrtyu
zbsdf
zsdfg
but when attempting to sort the following parsed text, free from all delimiters but worse “like-this” with a single hyphen or words with an apostrophe like “they’re”, it does not terminate:
Three days after the quarrel, Prince Stepan Arkadyevitch
Oblonsky–Stiva, as he was called in the fashionable world–
woke up at his usual hour, that is, at eight o’clock in the
morning, not in his wife’s bedroom, but on the leather-covered
sofa in his study. He turned over his stout, well-cared-for
person on the springy sofa, as though he would sink into a long
sleep again; he vigorously embraced the pillow on the other side
and buried his face in it; but all at once he jumped up, sat up
on the sofa, and opened his eyes.
“Yes, yes, how was it now?” he thought, going over his dream.
“Now, how was it? To be sure! Alabin was giving a dinner at
Darmstadt; no, not Darmstadt, but something American. Yes, but
then, Darmstadt was in America. Yes, Alabin was giving a dinner
on glass tables, and the tables sang, Il mio tesoro–not Il mio
tesoro though, but something better, and there were some sort of
little decanters on the table, and they were women, too,” he
remembered.
Again any help with this issue would be greatly appreciated!
Your
quickSortfunction will indeed recurse indefinitely:i,j,leftandrightare not modified anywhere in that function, so ifleft < rightthe function will be called recursively with the same parameters again and again.