Wednesday 11 January 2017

Quicksort utilizing Dutch National Flag Algorithm


Quicksort utilizing Dutch National Flag Algorithm 

 

 


Quicksort shows poor execution for data sources that contain many rehashed components. The issue is unmistakably obvious when all the information components are equivalent. At that point at every recursion, the left segment is vacant (no info qualities are not as much as the rotate), and the correct parcel has just diminished by one component (the turn is expelled). Thusly, the calculation sets aside quadratic opportunity to sort a variety of equivalent qualities.




To take care of this issue an option straight time segment routine can be utilized that isolates the qualities into three gatherings:

values not as much as the rotate

values equivalent to the rotate and

values more noteworthy than the rotate.

The qualities equivalent to the rotate are as of now sorted, so just the not exactly and more prominent than parcels should be recursively sorted. This straight time parcel routine is like three-path Partitioning for Dutch national banner issue.

C++ execution –


#include <bits/stdc++.h>
using namespace std;

// Partition routine using Dutch National Flag Algorithm
pair<int, int> Partition(int arr[], int start, int end)
{
    int mid = start;
    int pivot = arr[end];

    while (mid <= end)
    {
        if (arr[mid] < pivot)
        {
            swap(arr[start], arr[mid]);
            ++start, ++mid;
        }
        else if (arr[mid] > pivot)
        {
            swap(arr[mid], arr[end]);
            --end;
        }
        else
        {
            ++mid;
        }
    }

    // arr[start .. mid - 1] contains all occurrences of pivot
    return make_pair(start - 1, mid);
}

// Quicksort routine
void QuickSort(int arr[], int start, int end)
{
    // base condition for 0 or 1 elements
    if (start >= end)
        return;

    // handle 2 elements separately as Dutch national flag
    // algorithm will work for 3 or more elements
    if (start - end == 1)
    {
        if (arr[start] < arr[end])
            swap(arr[start], arr[end]);
        return;
    }

    // rearrange the elements across pivot using Dutch
    // national flag problem algorithm
    pair<int, int> pivot = Partition(arr, start, end);

    // recurse on sub-array containing elements that are less than pivot
    QuickSort(arr, start, pivot.first);

    // recurse on sub-array containing elements that are more than pivot
    QuickSort(arr, pivot.second, end);
}

// main function
int main()
{
    int arr[] = { 2, 6, 5, 2, 6, 8, 6, 1, 2, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);

    QuickSort(arr, 0, n - 1);

    // print the sorted array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}


Yield:

1 2 5 6 8

The best case for the calculation now happens when all components are equivalent (or are looked over a little arrangement of k « n components). On account of every equivalent component, the altered quicksort will perform at most two recursive approaches purge subarrays and in this manner complete in direct time.
Previous Post
Next Post

0 comments: