Data Structure and Algorithm Basis (Wang Zhuo) (35): Quick Sort by Exchange Sorting [Phase 2: Standard Answers, Preliminary Discovery of Problems]

Table of contents

Phase Two: Divide in Two

The program of the entire quicksort algorithm runs in a large frame:

Changes made (differences from the original program):

 Project 1:

PPT standard answer:

 Project 1 small problem:

 There is also a huge problem with Project 1:

Specific issues:

Specifically dig deep to solve the problem and build the next section



Phase Two: Divide in Two

Perform the traversal and sorting operations on the two sub-tables that the table is divided into, and then divide them into more subdivided sub-tables

Until the subtable unit is 1 (one element)

Yes, what he said makes sense, the operation I wrote above,

In fact, only the first element of the entire table can be operated, which is a bit inflexible

Modify it based on the program written above according to his ideas, and supplement the entire framework program of the complete quick sort algorithm:


The program of the entire quicksort algorithm runs in a large frame:

  1. Determine the center
  2. Traverse the first half of the table (nested recursion we operate on the entire table algorithm)
  3. Traverse the second half of the table

Changes made (differences from the original program):

Pass in int low, int high two elements:

Determine (limit) the range of operational changes that can be made within this cycle of the entire program


The initial value of Lr[0] is changed to Lr[low]:


Let this traversal function become more flexible, and it is not necessary to compare and traverse only the first element every time


The functionality of the function is customized as:

  • The data range of the operation is: high to low
  • The object used as a program comparison traversal is: the initial value of low passed in

 Project 1:

int 遍历(SqList &L, int low, int high)
{
    L.r[0] = L.r[low];
    while (low < high)
    {
        if (L.r[high].key < L.r[0].key)
        {
            L.r[low] = L.r[high];
            low++;
        }
        else
            high--;
        if (L.r[0].key < L.r[low].key)
        {
            L.r[high] = L.r[low];
            high--;
        }
        else
            low++;
    }
    L.r[low] = L.r[high] = L.r[0];
    return low;
}

void QuickSort(SqList& L, int low, int high)
{
    int pivot = 遍历(L, low, high);
    QuickSort(L, low, pivot-1);
    QuickSort(L, pivot + 1, high);
}

Then at this time, I thought that what we wrote was almost done, and then I went to see the standard answer:


PPT standard answer:

int Partition(SqList& L, int low, int high)
{
    L.r[0] = L.r[low];  //复制到哨兵位置
    KeyType pivotkey = L.r[low].key;
    while (low < high) 
    {
        while (low < high && L.r[high].key >= pivotkey)
            high--;  
        L.r[low] = L.r[high];

        while (low < high && L.r[low].key < pivotkey)
            low++;
        L.r[high] = L.r[low];

    }
    L.r[low] = L.r[0];
    return low;
}

void QuickSort(SqList& L, int low, int high) {
    if (low < high)
    {
        int pivotloc = Partition(L, low, high);  //将L一份为二
        QuickSort(L, low, pivotloc - 1);  //对低子表递归排序
        QuickSort(L, pivotloc + 1, high);  //对高子表递归排序
    }
}

OK, I didn't take it seriously at first, but at first glance there is no big problem, only one harmless


 Project 1 small problem:

Supplementary conditional statement: the fast sorting requirement must be (low < high)

As a result, I tried it out and brought it into the example, and found


 There is also a huge problem with Project 1:

Specific issues:

For the problem of operating logic mechanism, let's take the example on the PPT as an example:

Convert each specific operation into a table and display as follows: 

the first few steps operate Low points to High points to
step 1 49 Change Sentinel 1 8
step 2

49' do not move, high-- (the first if / else judgment)

1 7
step 3 49 does not move, low++ (the second if / else judgment) 2 7


Here comes the third step, and obviously something goes wrong:

There is no element in the space yet, your algorithm starts to overtake him and jump to the next space, why?

The repetition is relatively harmless, but the low moves, dangerous! ! ! (going wrong)

Clearly here, we can see that the problem is:

After the first processed element is placed in the sentinel, we compare the sentinel with the element itself

That is, comparing yourself and missing this space;

Instead of using this space to fit the first element smaller than the sentinel

See the next section for specific digging to solve the problem

Guess you like

Origin blog.csdn.net/Zz_zzzzzzz__/article/details/130514255