Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QuickSelect with Hoare partition scheme

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 ?

like image 754
dragosb Avatar asked Oct 10 '19 22:10

dragosb


People also ask

What is Hoare partition?

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.

Is Hoare partition stable?

O(n log(n)). Like others, Hoare's partitioning doesn't produce a stable sort.

What is Lomuto partitioning?

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.

Which type of partitioning is used in Quicksort?

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.

How do you partition in Hoare partition scheme?

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.

What are the disadvantages of Hoare partitioning?

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.

Why does quicksort degrade to O(n2)?

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?

What is QuickSelect? QuickSelect is a selection algorithm to find the K-th smallest element in an unsorted list.


1 Answers

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]
}
like image 191
rcgldr Avatar answered Oct 06 '22 14:10

rcgldr