void database::quickSortInt(int top,int bottom, char order)
{
// top = subscript of beginning of array
// bottom = subscript of end of array
int middle;
if (top < bottom)
{
middle = partitionInt(top, bottom,order);
quickSortInt(top,middle,order); // sort first section
quickSortInt(middle+1,bottom,order); // sort second section
}
return;
}
int database::partitionInt(int top, int bottom, char order)
{
int x = indexField[top];
int i = top - 1;
int j = bottom + 1;
int temp;
if(order == 'd' || order == 'D')
{
do
{
do
{
j--;
}while (x >indexField[j]);
do
{
i++;
}while (x <indexField[i]);
if (i < j)
{
temp = indexField[i];
indexField[i] = indexField[j];
indexField[j] = temp;
temp = primaryKey[i];
primaryKey[i] = primaryKey[j];
primaryKey[j] = temp;
}
}while (i < j);
return j; // returns middle subscript
}
}
I got my Quick Sorting algorithm working but I'm having problems recreating it in ascending order. If anyone could offer any suggestions it would be much appreciated.
I got it to go into descending order but for some reason its returning some random negative huge number. Ex. -3945934 and I'm missing my last index value.
I figure it's because my array is off by one to the left and it's accessing outside the array boundaries however i can't seem to figure out why it is doing so.
void database::quickSortInt(int top,int bottom, char order)
{
// top = subscript of beginning of array
// bottom = subscript of end of array
int middle;
if (top < bottom)
{
middle = partitionInt(top, bottom,order);
quickSortInt(top,middle,order); // sort first section
quickSortInt(middle+1,bottom,order); // sort second section
}
return;
}
int database::partitionInt(int top, int bottom, char order)
{
int x = indexField[top];
int i = top - 1;
int j = bottom + 1;
int temp;
if(order == 'd' || order == 'D')
{
do
{
do
{
j--;
}while (x <indexField[j]); //just had to reverse
do
{
i++;
}while (x >indexField[i]); // just had to reverse
if (i < j)
{
temp = indexField[i];
indexField[i] = indexField[j];
indexField[j] = temp;
temp = primaryKey[i];
primaryKey[i] = primaryKey[j];
primaryKey[j] = temp;
}
}while (i < j);
return j; // returns middle subscript
}
}
I believe i fixed it my J= was storing something outside of the array because of the bottom+1. I removed it and it fixed as far as i can tell for now. If anyone thinks their is something else wrong just let me know but for now its off to bed.