简体   繁体   中英

Quicksort not working with large array

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);

Output on screen: 在此输入图像描述

When debugging the application an exception appears with the following:

Access violation writing location 0x00140F64

#define SIZE 356

通话次数 异常弹出窗口

您可以通过将堆栈保留大小(在配置属性>链接器>系统中)设置为更高的值来增加堆栈大小,但如果递归太深,这将无济于事。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM