edmund1978
Programmer
I'm trying to adapt a quicksort class from sorting arrays to sorting vectors. I've used this for arrays, and it works (was taken from textbook) but I get a memory error when I try it with vectors. Any ideas?
#include <vector>
using std::vector;
class myQuickSort
{
public:
void sortArray(vector<int> a, int i, int j);
inline void exchange(vector<int> b, int i, int j);
int partition(vector<int> a, int low, int high);
};
void myQuickSort::sortArray(vector<int> a, int i, int j)
{
//int partition(int a[], int, int);
if (i >= j || i < 0)
return;
int k = partition(a, i, j);
sortArray(a, i, k-1);
sortArray(a, k+1, j);
}
int myQuickSort:
artition(vector<int> a, int low, int high)
{
register int pe;
//int pe;
int i = low;
int j = high;
exchange(a, (i+j)/2, j);
pe = a[j];
while (i < j)
{
while (i<j && a <= pe) i++;
while (i<j && a[j] >= pe) j--;
if (i<j) exchange(a, i++, j);
}
if (i != high) exchange(a,i,high);
return i;
}
inline void myQuickSort::exchange(vector<int> b, int i, int j)
{
int t = b[j];
b[j] = b;
b = t;
}
#include <vector>
using std::vector;
class myQuickSort
{
public:
void sortArray(vector<int> a, int i, int j);
inline void exchange(vector<int> b, int i, int j);
int partition(vector<int> a, int low, int high);
};
void myQuickSort::sortArray(vector<int> a, int i, int j)
{
//int partition(int a[], int, int);
if (i >= j || i < 0)
return;
int k = partition(a, i, j);
sortArray(a, i, k-1);
sortArray(a, k+1, j);
}
int myQuickSort:
{
register int pe;
//int pe;
int i = low;
int j = high;
exchange(a, (i+j)/2, j);
pe = a[j];
while (i < j)
{
while (i<j && a <= pe) i++;
while (i<j && a[j] >= pe) j--;
if (i<j) exchange(a, i++, j);
}
if (i != high) exchange(a,i,high);
return i;
}
inline void myQuickSort::exchange(vector<int> b, int i, int j)
{
int t = b[j];
b[j] = b;
b = t;
}