简体   繁体   中英

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 .

When partition method is called, I understand that it's checking the array index value in the following loop 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. 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.

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. 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. 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 if (a[left] < pivot ) 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 . Here we will have left = 2 and right = 5 ... So we swap those two elements. And make partition at left as we are sure that all elements to left are smaller then left.

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