Is it possible to implement QuickSelect algorithm using Hoare partitioning?
At least at first glance it seems that it cannot be done because Hoare partitioning does not return the index of the pivot necessarily.
Am I missing something ?
Hoare partition is an algorithm that is used to partition an array about a pivot. All elements smaller than the pivot are on it's left (in any order) and all elements greater than the pivot are on it's right (in any order). Quicksort algorithm uses hoare paritition to partition the array.
O(n log(n)). Like others, Hoare's partitioning doesn't produce a stable sort.
What Is the Lomuto Partition Algorithm? In Lomuto's partition, the pivot element is typically chosen to be the last element. Once sorted in its rightful place, this pivot element is no longer considered a participant in the subsequent recursive calls for subarrays.
Hoare Partitioning. Hoare partitioning was proposed by Tony Hoare when the Quicksort algorithm was originally published. Instead of working across the array from low to high, it iterates from both ends at once towards the center.
Hoare Partition Scheme Hoare uses two indices that start at the ends of the array being partitioned, then move toward each other until they detect an inversion: a pair of elements, one greater than the pivot, one smaller, in the wrong order relative to each other. The inverted elements are then swapped.
Like Lomuto’s partition scheme, Hoare partitioning also causes Quick sort to degrade to O (n^2) when the input array is already sorted, it also doesn’t produce a stable sort.
But like Lomuto’s partition scheme, Hoare partitioning also causes Quicksort to degrade to O(n2)when the input array is already sorted; it also doesn’t produce a stable sort.
What is QuickSelect? QuickSelect is a selection algorithm to find the K-th smallest element in an unsorted list.
With Hoare partition scheme, since the pivot or elements equal to the pivot can end up anywhere after a partition step, the base (terminating) case occurs when the partition size is reduced to a single element. Example code. QuickSelectr is the actual function. QuickSelect validates the parameters.
int QuickSelectr(int a[], int lo, int hi, int k )
{
if (lo == hi) // recurse until lo == hi
return a[lo];
int p = a[(lo+hi)/2]; // Hoare partition
int i = lo - 1;
int j = hi + 1;
while (1){
while (a[++i] < p);
while (a[--j] > p);
if (i >= j)
break;
std::swap(a[i], a[j]);
}
if(k <= j)
return QuickSelectr(a, lo, j-0, k); // include a[j]
else
return QuickSelectr(a, j+1, hi, k); // exclude a[j]
}
// parameter check
int QuickSelect(int *a, int lo, int hi, int k)
{
if(a == (int *)0 || k < lo || k > hi || lo > hi)
return 0;
return QuickSelectr(a, lo, hi, k);
}
Using i instead of j for the split:
int QuickSelectr(int a[], int lo, int hi, int k )
{
if (lo == hi) // recurse until lo == hi
return a[lo];
int p = a[(lo+hi+1)/2]; // Carefully note the +1 compared
// to the variant where we use j
int i = lo - 1;
int j = hi + 1;
while (1){
while (a[++i] < p);
while (a[--j] > p);
if (i >= j)
break;
std::swap(a[i], a[j]);
}
if(k < i)
return QuickSelectr(a, lo, i-1, k); // exclude a[i]
else
return QuickSelectr(a, i+0, hi, k); // include a[i]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With