2,016,408 algorithm topic

The basic idea: fast sequencing method modeled based on the n integers pivot division process.

If i = n / 2, then the packet is completed, ends the algorithm

If i <n / 2, the front pivot elements belong a1, i continue after dividing element

If i> n / 2, the following elements belong to pivot a2, continue dividing elements before i

int set_partition(int a[],int n)
{
    int pivotkey,low=0,low0=0,high=n-1,high0=n-1,flag=1,k=n/2,i;
    int s1=0,s2=0;
    while (flag) // selected pivot
    {
        pivotkey = a [0];
        while(low<high)
        {
            while(low<high&&a[high]>=pivotkey)--high;
            if(low!=high)a[low]=a[high];
            while(low<high&&a[low]<=pivotkey)++low;
            if(low!=high)a[high]=a[low];

        }
        a[low]=pivotkey;
        if (low == k-1) // If the pivot axis is n / 2 small element division is successful
            flag=0;
        else{
            if(low<k-1)
            {
                low0=++low;
                high=high0;
            }
            else{
                high0=--high;
                low=low0;
            }
        }
    }
    for(i=0;i<k;i++)s1+=a[i];
    for(i=0;i<k;i++)s2+=a[i];
    return s2-s1;
}

  

Guess you like

Origin www.cnblogs.com/yangmenda/p/11707511.html