简体   繁体   English

我不明白这种快速排序的实现

[英]I don't understand this quicksort implementation

I have a code for quicksort in C++ that works perfectly for an array of non-unique elements. 我有一个C ++中的quicksort代码,可以完美地用于非唯一元素数组。 I'm sure that a lot of people here knows it, but, who does understand it? 我敢肯定,这里有很多人都知道,但是,谁能理解呢? Let me explain myself better. 让我更好地解释自己。 This is the code: 这是代码:

void quicksort(int a[], int first, int last){
    int i,j;
    int pivot;

    if((last -first + 1) <= 1) return;

    pivot = a[(first+last) / 2];
    i = first;
    j = last;

    while(i <= j){
        while(a[i] < pivot) i++;
        while(a[j] > pivot) j--;

        if(i <= j){
            //SWAP
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;

            i++;
            j--;
        }
    }

    quicksort(a, first, j);
    quicksort(a,i, last);
}

So, i understand everything but the if on the swap. 所以,我了解一切,但如果在交换。 Can anyone tell me, mathematically, what is the exact case or set of cases where i > j after the two inner whiles? 谁能从数学上告诉我,在两个内部while之后i> j的确切情况或一组情况是什么? I know specific cases for it, but what is the mathematical (or exact) property of them for happening? 我知道具体情况,但是发生情况的数学(或精确)性质是什么?

Sorry for the crappy english, and thanks. 对不起,英语不好,谢谢。

PD: Ignore in this case optimizations, or choosing the pivot and all that stuff, please. PD:在这种情况下,请忽略优化,或者选择枢轴和所有其他东西。

If, when starting out, a[i] > pivot (so i is not changed) and a[j] > pivot for all j until a[j] = pivot, the next iteration of the loop will result in a situation where j < i. 如果在开始时a [i]>枢轴(所以我没有改变)并且a [j]>所有j的枢轴,直到a [j] =枢轴,则循环的下一次迭代将导致j <i。

To illustrate... 为了显示...

Take the following array: 采取以下数组:

int a[] = [10, 7, 2, 6, 3];

On the first call of quicksort, with first being 0 and last being 4 (last index in the array), pivot will be a[2] = 2. In the first iteration if the while loop, a[0] > 2, so i is not changed. 在快速排序的第一次调用中,第一个为0,最后一个为4(数组中的最后一个索引),数据透视将为a [2] =2。在第一次迭代中,如果while循环中a [0]> 2,则我没有改变。 a[4] > 2, j--, a[3] > 2, j--, a[2] = 2, now we hit the if statement. a [4]> 2,j--,a [3]> 2,j--,a [2] = 2,现在我们命中了if语句。 0 <= 2, so we swap a[0] and a[2] and execute i++ and j--. 0 <= 2,所以我们交换a [0]和a [2]并执行i ++和j--。

Now the array looks like this: 现在,数组如下所示:

[2, 7, 10, 6, 3]

with i = 1 and j = 1. a[i] > 2, so i is not changed. 当i = 1且j = 1时。a [i]> 2,因此i不变。 a[j] > 2, so j--, j now is 0. a[j] is not greater than 2 (since it is 2), and j stays at 0. Now, we have i = 1 and j = 0, or i > j. a [j]> 2,所以j--,j现在为0。a [j]不大于2(因为它是2),并且j保持为0。现在,我们有i = 1和j = 0 ,或i> j。

If you notice, the 2 is in it's "sorted" position and does not need to be moved anymore. 如果您注意到,则2处于“已排序”位置,因此不再需要移动。 Also, the pivot was the smallest element in the array. 同样,枢轴是数组中最小的元素。 Hope that helps you figure it out. 希望可以帮助您解决。

i and j start at either end of the array until they find a value that is greater than the pivot ( a[i] ) and less than the pivot ( a[j] ). ij从数组的任一端开始,直到找到一个大于枢轴( a[i] )且小于枢轴( a[j] )的值。 When two such values are found they are swapped locations so you end up with an array where after the loop i to the end are greater than the pivot, and the beginning to j is less than the pivot. 当找到两个这样的值时,它们将被交换位置,因此您将得到一个数组,其中在循环i至结尾之后,轴的起始点大于轴心,而j的起始点小于轴心。 Than we recurse on these two sub arrays. 比我们在这两个子数组上递归。

i>j when the list is done being divided by the pivot value. 列表完成后,将i>j除以数据透视值。 i and j have covered every value in the array to make sure it is on the correct side of the pivot. ij覆盖了数组中的每个值,以确保其位于轴的正确一侧。 This could happen right in the middle of the array or after only one value being swapped depending on where the pivot value is in the list. 这可能发生在数组中间,或者仅交换了一个值之后,具体取决于数据透视表值在列表中的位置。 The pivot could be the largest or the smallest value, or it could be right in the middle of the list of values. 支点可以是最大值或最小值,也可以正好在值列表的中间。

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

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