Quicksort Partitioning

Partitioning is the process by which we reorder our array so that elements with a value less than our pivot are moved to the left of the pivot and those with a larger value are moved to the right (see Figure 2.2). There are numerous manners in which we can do this. Here, we will describe an easy-to-understand scheme known as Lomuto Partitioning.

Take a look at this diagram:

Figure 2.2: Before and after partitioning of an array
Many other schemes exist. The Lomuto scheme has the drawback that it is not very performant when it is used on already-sorted lists. The original Hoare partition scheme performs better and works by having the array processed from both ends.The original Hoare scheme performs better as it does fewer swaps, although it also suffers from slow performance when a sorted list is used as input. Both the Lomuto and Hoare schemes result in non-stable sorting. A stable sort means that if two or more elements have the same key value, they will appear in the same input order as the sorted output. There are other schemes that can be used to make quick sort stable, but they utilize more memory.

To get a good perception on this partitioning scheme, it is best to simplify what the algorithm is doing in five simple steps, as follows:

  1. Pick the right most element of the array as the pivot.
  2. Start from the left and find the next element that is larger than the pivot.
  3. Swap this element with the next, which is smaller than pivot element.
  4. Repeat steps 2 and 3 until no more swapping is possible.
  5. Swap the first item which is larger than the pivot's value with the pivot itself.

To perform efficient partitioning using the steps mentioned, we can make use of two pointers, one pointing to the first item larger than the pivot value and the other used to search for the value that is smaller than pivot value.

In the following code, these are the integer pointers named x and i, respectively. The algorithm starts by choosing the pivot as the last item on the input array. It then processes the array from left to right in a single pass using the variable i. If the element currently being processed at i is smaller than the pivot, x is incremented and swapped. Using this technique, variable x is either pointing to a value larger than the pivot or the value of x is the same as i, in which case swapping will not modify the array. Once the loop exits, we perform the final step of swapping the first item that is larger than the pivot's value with the pivot. The code is as follows:

private int partition(int[] numbers, int start, int end) {
int pivot = numbers[end];
int x = start - 1;
for (int i = start; i < end; i++) {
if (numbers[i] < pivot) {
x++;
swap(numbers, x, i);
}
}

swap(numbers, x + 1, end);
return x + 1;
}
Snippet 2.7: Partitioning for quick sort. Source class name: QuickSort

Go to
https://goo.gl/vrStai to access the code.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.17.140