I’m trying to get quicksort working to sort an array of 7000 strings into alphabetical order, but all i’m getting is a blank output file. It works fine with my bubblesort method, but not with this. I’m sure it’s an obvious mistake, but i can’t pin-point it.
void ArrayStorage::quicksort(int first, int last, string list[])
{
int middle, p, index;
string temp, partition;
if (first < last)
{
middle = int(first + last)/2;
temp = list[middle];
list[middle] = list[first];
list[first] = temp;
partition = list[first];
p = first;
for (index = first + 1; index <= last; index++)
{
if(list[index] < partition)
{
p = p + 1;
temp = list[index];
list[index] = list[p];
list[p] = temp;
}
}
temp = list[first];
list[first] = list[p];
list[p] = temp;
quicksort(first, p - 1, list);
quicksort(p + 1, last, list);
}
}
I call the method like this:
quicksort(0,GetSize() -1,namesArray);
How about using the built in quicksort?:
std::sort(&namesArray[0], &namesArray[GetSize()]);