I am trying to implement a quicksort algorithm with Hoare partioning which can work with a large set of array. It works with small size arrays however when creating a large array the application stops working. Can anyone help?
The implementation looks as followed: QuickSort.h
void SerialQuicksort(T* first, T* last, T* myArray, int size) {
if (last - first>1) {
T* mid = ModifiedHoare(myArray, first, last);
SerialQuicksort(first, mid+1, myArray, size);
SerialQuicksort(mid+1, last, myArray, size);
}
}
Partitioning function:
int* ModifiedHoare(int* a, int* p, int* r)
{
if (*p>*r)
swapp(p, r); // Sentinel at both ends
int x = *p; // x stores the pivot and location p is vacant now.
while (1)
{
do{
*r--;
} while (*r>x); // search the smaller element in right
// portion.
*p = *r; // location r is vacant now.
do{
*p++;
} while (*p<x); // search the larger element in left portion.
if (p<r)
*r = *p; // location p is vacant now.
else{
if (*(r + 1) <= x)
r++;
*r = x;
return r; // return the location of the pivot
}
}
}
inline void swapp(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
Test - Main
#define SIZE 20000
int* myArray = new int[SIZE];
for (int i = 0; i < SIZE; i++)
{
*(myArray + i) = rand();
}
parallelQuick.SerialQuicksort(&myArray[0], &myArray[SIZE-1], myArray, SIZE);
When debugging the application an exception appears with the following:
Access violation writing location 0x00140F64
#define SIZE 356