Homework/HW09/Sort.cpp
void dualPivotQuickSort(int a[], int left, int right, int div)
{
    int len = right - left;

    if (len < LIS_THRESHOLD)
    { // insertion sort for small arrays
        for (int i = left + 1; i <= right; i++)
            for (int j = i; j > left && a[j] < a[j - 1]; j--)
                swap(a, j, j - 1);
        return;
    }
    int third = len / div;

    // "medians"
    int m1 = left + third;
    int m2 = right - third;

    if (m1 <= left)
        m1 = left + 1;
    if (m2 >= right)
        m2 = right - 1;
    if (a[m1] < a[m2])
    {
        swap(a, m1, left);
        swap(a, m2, right);
    }
    else
    {
        swap(a, m1, right);
        swap(a, m2, left);
    }

    // pivots
    int pivot1 = a[left];
    int pivot2 = a[right];

    // pointers
    int less = left + 1;
    int great = right - 1;

    // sorting
    for (int k = less; k <= great; k++)
    {
        if (a[k] < pivot1)
            swap(a, k, less++);
        else if (a[k] > pivot2)
        {
            while (k < great && a[great] > pivot2)
                great--;

            swap(a, k, great--);

            if (a[k] < pivot1)
                swap(a, k, less++);
        }
    }
    // swaps
    int dist = great - less;

    if (dist < 13)
        div++;
    swap(a, less - 1, left);
    swap(a, great + 1, right);

    // subarrays
    dualPivotQuickSort(a, left, less - 2, div);
    dualPivotQuickSort(a, great + 2, right, div);

    // equal elements
    if (dist > len - 13 && pivot1 != pivot2)
    {
        for (int k = less; k <= great; k++)
        {
            if (a[k] == pivot1)
                swap(a, k, less++);
            else if (a[k] == pivot2)
            {
                swap(a, k, great--);

                if (a[k] == pivot1)
                    swap(a, k, less++);
            }
        }
    }
    // subarray
    if (pivot1 < pivot2)
        dualPivotQuickSort(a, less, great, div);
}
/****************************** dualPivotQuickSort *************************************************
Action:         Dual pivot quicksort by Vladimir Yaroslavskiy.

Parameters:
IN:             int a[], holds pointer to the array of data to be sorted.
IN:             int left, holds the left most element number this instance will work on.
IN:             int right, holds the right most element number this instance will work on.
IN:             int div, holds the number of sections the array will be divided into.
OUT:            int a[], holds pointer to the sorted array.

Returns:        Nothing

Precondition:   <a> should be an allocated array, and left and right should be elements within that
                array.
***************************************************************************************************/
dualPivotQuickSort
Action:         Dual pivot quicksort by Vladimir Yaroslavskiy.

int a[], holds pointer to the array of data to be sorted.
IN:             int left, holds the left most element number this instance will work on.
IN:             int right, holds the right most element number this instance will work on.
IN:             int div, holds the number of sections the array will be divided into.
OUT:            int a[], holds pointer to the sorted array.

Nothing

 should be an allocated array, and left and right should be elements within that
array.
dualPivotQuickSort Action : Dual pivot quicksort by Vladimir Yaroslavskiy . int a [] holds pointer to the array of data to be sorted . IN : int left holds the left most element number this instance will work on . IN : int right holds the right most element number this instance will work on . IN : int div holds the number of sections the array will be divided into . OUT : int a [] holds pointer to the sorted array . Nothing should be an allocated array and left and right should be elements within that array .
void dualPivotQuickSort(int a[], int left, int right, int div)
{
    int len = right - left;
    if (len < LIS_THRESHOLD)
    { 
        for (int i = left + 1; i <= right; i++)
            for (int j = i; j > left && a[j] < a[j - 1]; j--)
                swap(a, j, j - 1);
        return;
    }
    int third = len / div;
    int m1 = left + third;
    int m2 = right - third;
    if (m1 <= left)
        m1 = left + 1;
    if (m2 >= right)
        m2 = right - 1;
    if (a[m1] < a[m2])
    {
        swap(a, m1, left);
        swap(a, m2, right);
    }
    else
    {
        swap(a, m1, right);
        swap(a, m2, left);
    }
    int pivot1 = a[left];
    int pivot2 = a[right];
    int less = left + 1;
    int great = right - 1;
    for (int k = less; k <= great; k++)
    {
        if (a[k] < pivot1)
            swap(a, k, less++);
        else if (a[k] > pivot2)
        {
            while (k < great && a[great] > pivot2)
                great--;
            swap(a, k, great--);
            if (a[k] < pivot1)
                swap(a, k, less++);
        }
    }
    int dist = great - less;
    if (dist < 13)
        div++;
    swap(a, less - 1, left);
    swap(a, great + 1, right);
    dualPivotQuickSort(a, left, less - 2, div);
    dualPivotQuickSort(a, great + 2, right, div);
    if (dist > len - 13 && pivot1 != pivot2)
    {
        for (int k = less; k <= great; k++)
        {
            if (a[k] == pivot1)
                swap(a, k, less++);
            else if (a[k] == pivot2)
            {
                swap(a, k, great--);
                if (a[k] == pivot1)
                    swap(a, k, less++);
            }
        }
    }
    if (pivot1 < pivot2)
        dualPivotQuickSort(a, less, great, div);
}
void dualPivotQuickSort ( int a [ ] , int left , int right , int div ) { int len = right - left ; if ( len < LIS_THRESHOLD ) { for ( int i = left + 1 ; i <= right ; i ++ ) for ( int j = i ; j > left && a [ j ] < a [ j - 1 ] ; j -- ) swap ( a , j , j - 1 ) ; return ; } int third = len / div ; int m1 = left + third ; int m2 = right - third ; if ( m1 <= left ) m1 = left + 1 ; if ( m2 >= right ) m2 = right - 1 ; if ( a [ m1 ] < a [ m2 ] ) { swap ( a , m1 , left ) ; swap ( a , m2 , right ) ; } else { swap ( a , m1 , right ) ; swap ( a , m2 , left ) ; } int pivot1 = a [ left ] ; int pivot2 = a [ right ] ; int less = left + 1 ; int great = right - 1 ; for ( int k = less ; k <= great ; k ++ ) { if ( a [ k ] < pivot1 ) swap ( a , k , less ++ ) ; else if ( a [ k ] > pivot2 ) { while ( k < great && a [ great ] > pivot2 ) great -- ; swap ( a , k , great -- ) ; if ( a [ k ] < pivot1 ) swap ( a , k , less ++ ) ; } } int dist = great - less ; if ( dist < 13 ) div ++ ; swap ( a , less - 1 , left ) ; swap ( a , great + 1 , right ) ; dualPivotQuickSort ( a , left , less - 2 , div ) ; dualPivotQuickSort ( a , great + 2 , right , div ) ; if ( dist > len - 13 && pivot1 != pivot2 ) { for ( int k = less ; k <= great ; k ++ ) { if ( a [ k ] == pivot1 ) swap ( a , k , less ++ ) ; else if ( a [ k ] == pivot2 ) { swap ( a , k , great -- ) ; if ( a [ k ] == pivot1 ) swap ( a , k , less ++ ) ; } } } if ( pivot1 < pivot2 ) dualPivotQuickSort ( a , less , great , div ) ; }
dualPivotQuickSort
Action:         Dual pivot quicksort by Vladimir Yaroslavskiy.
dualPivotQuickSort Action : Dual pivot quicksort by Vladimir Yaroslavskiy .
// insertion sort for small arrays // "medians" // pivots // pointers // sorting // swaps // subarrays // equal elements // subarray