I am having some problems with my quick sort algorithm. When I comment out my last recursive call. It works fine, but when I uncomment my last recursive call. My variable iDown prints out a really large negative number. Which leads me to believe that it is going outside of my array. I have been trying to resolve this, but no luck yet. Any advice you can all offer would be greatly appreciated.
Thanks
#include <iostream>
#include <ctime>
using namespace std;
#include "Sort.h"
Sort::Sort(int numOfValues)
{
mpSorted = NULL;
mpRandom = NULL;
fillRandom(10, 20);
}
Sort::~Sort()
{
delete [] mpRandom;
}
int Sort::fillRandom(int nNums, int numRange)
{
mpRandom = new int [nNums];
mpSorted = new int [nNums];
mSize = nNums;
for(int i = 0; i < nNums; i++)
{
mpRandom[i] = rand() % numRange + 1;
}
return 0;
}
void Sort::bubbleSort()
{
utilCopy();
utilBubbleSort(mpSorted, mSize);
}
void Sort::selectionSort()
{
utilCopy();
utilSelectionSort(mpSorted, mSize);
}
void Sort::insertionSort()
{
utilCopy();
utilInsertionSort(mpSorted, mSize);
}
void Sort::quickSort()
{
utilCopy();
utilQuickSort(mpSorted, 0, mSize - 1);
}
void Sort::print()const
{
utilPrint(mpSorted, mSize);
}
int Sort::utilCopy()
{
for(int i = 0; i < mSize; i++)
{
mpSorted[i] = mpRandom[i];
}
return 0;
}
int Sort::utilPrint(int num[], int size)const
{
for(int i = 0; i < size; i++)
cout << num[i] << endl;
cout << "Value of size " << size << endl;
return 0;
}
int Sort::utilBubbleSort(int num[], int size)
{
bool swapMade = true;
int last = size - 1;
int swap;
long int startSeconds = time(NULL);
// Perform the Bubble Sort algorithm
for (int pass=0; pass<size && swapMade; pass++)
{
swapMade = false;
for (int i=0; i<last; i++)
{
if (mpSorted[i] > mpSorted[i+1])
{
swap = mpSorted[i];
mpSorted[i] = mpSorted[i+1];
mpSorted[i+1] = swap;
swapMade = true;
}
}
last--;
}
long int stopSeconds = time(NULL);
cout << "It took " << stopSeconds - startSeconds << "to complete the sort" << endl;
return 0;
}
int Sort::utilSelectionSort(int num[], int size)
{
int i;
int j;
int smallest;
int swap;
// Perform the Selection Sort algorithm
for (i=0; i<(size-1); i++)
{
smallest = i;
for (j=i+1; j<size; j++)
if (mpSorted[j] < mpSorted[smallest])
smallest = j;
swap = mpSorted[i];
mpSorted[i] = mpSorted[smallest];
mpSorted[smallest] = swap;
}
return 0;
}
int Sort::utilInsertionSort(int num[], int size)
{
int i;
int j;
int swap;
// Perform the Insertion Sort algorithm
for (i=1; i<size; i++)
{
swap = mpSorted[i];
for (j=i; (j>0) && (swap < mpSorted[j-1]); j--)
mpSorted[j] = mpSorted[j-1];
mpSorted[j] = swap;
}
return 0;
}
int Sort::utilQuickSort(int num[], int start, int end)
{
if(start == end)
return 0;
if(start >= end)
return 0;
int iUp = start;
int iDown = end;
int iPivot = num[(start + end) / 2];
cout << "Value of iPivot " << iPivot << endl;//Debug statement
for(;;)/>
{
while(num[iUp] <= iPivot)
iUp++;
while(num[iDown] >= iPivot)
iDown--;
if(iUp <= iDown)
{
int tmp = num[iUp];
num[iUp] = num[iDown];
num[iDown] = tmp;
}
else
break;
cout << "Value of iUp " << iUp << endl;//Debug statement
cout << "Value of iDown " << iDown << endl;//Debug statement
};
cout << "Value of num[iUp] " << num[iUp] << endl;//Debug statement
cout << "Value of num[iDown] " << num[iDown] << endl;//Debug statement
utilQuickSort(num, start, iDown - 1);
//utilQuickSort(num, iDown + 1, end);
return 0;
}

New Topic/Question
Reply




MultiQuote






|