简体   繁体   English

Quicksort分区功能的参数

[英]Arguments to Partition function for Quicksort

So I've been attempting to write a stack-based quicksort function, which calls a partition function. 因此,我一直在尝试编写基于堆栈的quicksort函数,该函数调用分区函数。 The header for partition() is as follows: partition()的标头如下:

int partition(void **A, int n, void *pivot, int (cmp)(void *, void *));

where A is the array, n is the size of the array and pivot is the value of the pivot (not the index). 其中,A是数组,n是数组的大小,pivot是枢轴的值(不是索引)。

My current call to partition is: 我当前对partition的调用是:

partition(&A[low_int], high_int + 1, A[low_int+(high_int-low_int)/2], cmp)

Above, my values for low and high are the classic 'l' and 'h' used in iterative quicksort, where l begins as the lowest index (and h the highest). 上面,我的低和高值是迭代快速排序中使用的经典“ l”和“ h”,其中l从最低索引开始,而h从最高索引开始。 These values then change as the function continues. 然后,这些值随着函数的继续而改变。

I'll post my partition function below: 我将在下面发布我的分区函数:

int 
partition(void **A, int n, void *pivot, int (cmp)(void *, void *)) {
    int k;

    int i = 0;
    for (int j = 0; j <= n-2; j++) {
        if (cmp(A[j], pivot) <= 0) {
            i++;
            swap(A, i, j);
        }
    }
    swap(A, i+1, n-1);
    k = i + 2;
    return k; //k is the first value after the pivot in partitioned A

} }

My problem is deciding on the inputs for my call to partition(). 我的问题是决定我对partition()的调用的输入。 For the first argument, I've chose &A[low_int], as I'm not using a "left" as one of my inputs and therefore am trying to create a pointer to essentially start my array later. 对于第一个参数,我选择了&A [low_int],因为我没有使用“ left”作为输入之一,因此正在尝试创建一个指针,以稍后实质上启动数组。 The third argument if for pivot, where I've been trying to select an element within that range, but both this and argument 1 have been causing my code to either return an unsorted array or run infinitely. 第三个参数(如果是透视),我一直在尝试选择该范围内的元素,但是此参数和参数1一直导致我的代码返回未排序的数组或无限运行。

Could I please get some help here with what I've done wrong and how to fix it? 请问这里有我做错了什么以及如何解决它的帮助吗?

I've tried to include all relevant code, but please let me know if I've missed anything important or if anything I've written is unclear, or you need more info. 我已尝试包含所有相关代码,但是如果我错过了任何重要内容,或者不清楚我写的内容,还是需要更多信息,请告诉我。 Thank you 谢谢

Consider what happens if low_int is 1000 and high_int is 2000 and the array ends at 2000. Now you give it the array B = &A[1000] and the value 2001. The value of 2001 causes it to access the element B[2001-1] = B[2000] = A[3000] . 考虑一下low_int为1000且high_int为2000且数组在2000处结束时会发生什么。现在给它提供数组B = &A[1000]和值2001。值2001使它访问元素B[2001-1] = B[2000] = A[3000] It's accessing the array out of bounds. 它正在访问数组。

Shouldn't you use something like high_int - low_int + 1 for the second argument? 您不应该为第二个参数使用诸如high_int - low_int + 1类的东西吗? Note: I haven't verified that your code doesn't have off-by-one errors with the argument high_int - low_int + 1 but anyway it seems to me that you should be substracting low_int from high_int . 注:我还没有验证您的代码没有关闭的情况的一个错误的说法high_int - low_int + 1 ,但无论如何,在我看来,你应该从其减去low_inthigh_int

Another option would be to give it A , low_int and high_int . 另一种选择是给它Alow_inthigh_int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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