简体   繁体   English

了解以下Quicksort实施

[英]Understanding the following Quicksort Implementation

I have the following two methods : 我有以下两种方法:

public static void QuickAlgo(int[] a, int left, int right) {

        int index = partition(a, left, right);

        if (left < index - 1) {
            QuickAlgo(a, left, index - 1);
        }
        if (index < right) {
            QuickAlgo(a, index, right);
        }

    }

    static int partition(int[] a, int left, int right) {

        int pivot = a[(left + right) / 2];

        while (left <= right) {

            while (a[left] < pivot) {
                left++;
            }
            while (a[right] > pivot) {
                right--;
            }

            if (left <= right) {

                int temp = a[left];
                a[left] = a[right];
                a[right] = temp;
                left++;
                right--;

            }

        }

        return left;
    }

Suppose I have the following unsorted data for the above problem: 假设对于上述问题,我具有以下未排序的数据:

_______________________________________
65  | 72 | 23 | 36 | 99 | 20 | 1 | 44 |

Let's say, my pivot here is 36 . 假设,我在这里的枢纽是36

When partition method is called, I understand that it's checking the array index value in the following loop while(left <= right) 当调用partition method时,我了解它正在以下循环中检查数组索引值while(left <= right)

Now, for the following code snippet: 现在,对于以下代码段:

if (left <= right) {

                int temp = a[left];
                a[left] = a[right];
                a[right] = temp;
                left++;
                right--;

            }

Shouldn't it be like this: 不应该是这样的:

1) a[left] should be compared to pivot first , as in my example 65 > 36 and hence eligible for swap. 1)应该先比较a [left]与数据透视,例如在我的示例65> 36中,因此有资格进行交换。 Similarly, pivot must be compared with the a[right] , in my example, it's 44 , and since pivot(36) < 44, pointer decrements and moved to 1 and hence 1 is eligible for swapping because pivot(36) > 1. 同样,pivot必须与a [right]进行比较,在我的示例中为44,由于pivot(36)<44,指针递减并移动到1,因此1有资格进行交换,因为pivot(36)> 1。

Whereas the above code snippet that I have mentioned, is directly comparing the index values and swapping the numbers. 而我上面提到的上述代码片段是直接比较索引值并交换数字。

May be I am not understanding something here. 可能是我在这里不了解某些内容。 Could anyone please explain? 有人可以解释吗?

Thanks 谢谢

When you get to the point of swapping, the comparisions with the pivot you've mentioned have already been done. 当谈到交换点时,与您提到的枢轴的比较已经完成。 The first while loop ends when it has found a value greater than the pivot & the second loop ends when it has found a value lesser than the pivot. 第一个while循环在找到大于枢轴的值时结束,而第二个循环在发现小于枢轴的值时结束。 This means that both left & right pointer has found a value that are on the wrong side and thus, both values are eligible for swapping. 这意味着左右两个指针都找到了错误的值,因此这两个值都可以交换。

The if (left <= right) is only there to make sure that you don't swap the values once the left cursor has passed the right cursor. if (left <= right)仅用于确保一旦左光标经过右光标后就不交换值。 When the left cursor has passed the right cursor, it means the partioning is done and hence no more swapping should be done. 当左光标经过右光标时,表示分割已完成,因此不再需要进行交换。

We are doing that to make sure that all the elements to left of pivot are less than pivot and towards right of pivot are greater than pivot. 我们这样做是为了确保枢轴左侧的所有元素都小于枢轴,而枢轴右侧的所有元素都大于枢轴。 Now if you see the two while loops before 现在,如果您看到两个while循环之前 if (a[left] < pivot ) 如果(a [left] <枢轴) they are for traversing till the point where an element is less than pivot from beginning and other for getting an element greater than pivot from end. 它们用于遍历到元素从起点开始小于枢轴的点,而另一个用于使元素从末端开始大于枢轴的点。 Now consider : 3 4 19 7 21 2 as our array and 7 is pivot . 现在考虑:3 4 19 7 21 2作为数组,7是枢轴。 Here we will have left = 2 and right = 5 ... So we swap those two elements. 在这里,我们将剩下left = 2和right = 5 ...因此,我们交换了这两个元素。 And make partition at left as we are sure that all elements to left are smaller then left. 并在左侧进行分区,因为我们确保左侧的所有元素都小于左侧。

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

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