QuickSort

    技术2022-05-19  17

     

    void QuickSort(vector<int> & a, int low, int high)

    {

    if (high - low <= 1)

    return;

     

    int i = low;

    int j = high - 1;

    int m = (i + j) / 2;

     

    Label1:

    for (; i < high; i++)

    {

    if (a[m] < a[i] && i != m)

    break;

    }

     

    for (; j >= low; j--)

    {

    if (a[j] < a[m] && j != m)

    break;

    }

     

    if (i < m)

    {

    if (j > m)

    {

    int temp1, temp3;

    temp1 = a[j];

    temp3 = a[i];

    a[i] = temp1;

    a[j] = temp3;

    }

    else 

    {

    if (i < j)

    {

    int temp1, temp2, temp3;

    temp1 = a[j];

    temp2 = a[m];

    temp3 = a[i];

     

    int tempm = j;

    int tempj = m;

     

    j = tempj;

    m = tempm;

     

    a[i] = temp1;

    a[m] = temp2;

    a[j] = temp3;

    }

    else

    {

    int temp2, temp3;

    temp2 = a[m];

    temp3 = a[i];

     

    int tempi = j;

    int tempm = i;

    int tempj = m;

     

    i = tempi;

    j = tempj;

    m = tempm;

     

    a[m] = temp2;

    a[j] = temp3;

    }

    }

    }

    else

    {

    if (i < j)

    {

    int temp1, temp2, temp3;

    temp1 = a[j];

    temp2 = a[m];

    temp3 = a[i];

     

    int tempi = m;

    int tempm = i;

    int tempj = j;

     

    i = tempi;

    j = tempj;

    m = tempm;

     

    a[i] = temp1;

    a[m] = temp2;

    a[j] = temp3;

    }

    else if (j > m && j < i)

    {

    int temp1, temp2;

    temp1 = a[j];

    temp2 = a[m];

    int tempi = m;

    int tempm = j;

    int tempj = i;

     

    i = tempi;

    j = tempj;

    m = tempm;

     

    a[i] = temp1;

    a[m] = temp2;

    }

    else

    {

    if (low < m)

    QuickSort(a, low, m);

    if (m + 1 < high)

    QuickSort(a, m + 1, high);

    return;

    }

    }

     

    i++;

    j--;

    if (i >= high) {i = high -1;} else {}

    if (j < low) {j = low;} else {}

     

    goto Label1;

    }

     


    最新回复(0)