Data Structure and Algorithm Basics (Wang Zhuo) (35): Quick Sort by Exchange Sorting [The first stage: the first traversal]

Table of contents

Quick sort:

Method one:

Method 2: (commonly used, important and difficult points)

Phase 1: First pass through

Project 1:

question:

Project 2:

question:

Project 3:

question:

Project 4:

Project 5:


Quick sort:

Method one:

The first sentry is placed, traversing from left to right: the big one is placed at the end, the small one is placed at the front, and the last sentry is placed in the middle

Continue to perform the same algorithmic operation on the new table (recursively)

Until the operation has traversed all elements

Method 2: (commonly used, important and difficult points)

 I won’t repeat the detailed ideas, you can watch the video of Mr. Wang Zhuo

Week 14 06--Chapter 8 Sorting 6--8.3 Exchange Sort 2--Quick Sort 1_哔哩哔哩_bilibili

Or directly look at the following code to understand ideas:


Phase 1: First pass through


Project 1:

void 遍历一遍(SqList L)
{
    int low, high;
    L.r[0] = L.r[1];
    low = 1;
    high = L.length;
    while (low < high)
    {
        if (L.r[high].key < L.r[0].key)
        {
            L.r[low] = L.r[high];
            low++;
        }
        if (L.r[0].key < L.r[low].key)
        {
            L.r[high] = L.r[low];
            high--;
        }
    }
}

By watching the video operation, we found that the program we wrote here has certain

question:

In the program statement we wrote:

When the element does not need to be moved, we do not perform any operations on the program

In this way, when the program has elements that do not need to be moved, our program starts to stagnate

Even when this situation occurs for both low and high, the program will directly enter an infinite loop and stop


Project 2:

void 遍历一遍(SqList L)
{
    int low, high;
    L.r[0] = L.r[1];
    low = 1;
    high = L.length;
    while (low < high)
    {
        if (L.r[high].key < L.r[0].key)
            L.r[low] = L.r[high];
        low++;
        if (L.r[0].key < L.r[low].key)
            L.r[high] = L.r[low];
        high--;
    }
}

question:

But in fact, it is not right for us to change/change the program to this way. In the program statement we wrote:

When we move the elements

We also need to move the low and high pointers

In this way, the next element we want (need to) move

There is no way to insert it into an element with a space (empty)


Project 3:

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

question:

Here, our programmatic changes are a bit overkill

In fact, because we are not clear about the process of program execution, our thinking is confused

As a result, the more we modify the program, the more mistakes we make:

When the element pointed to by the program needs to be moved, after the move:

The original pointer of the element does not move (as the next round of pointers to blank elements)


        if (L.r[high].key < L.r[0].key):

high不动

        if (L.r[0].key < L.r[low].key):

low does not move


Then:

The pointer to the location where the element is moved needs to be moved. I can’t compare the element you just moved in the last round:


        if (L.r[high].key < L.r[0].key):

low++


        if (L.r[0].key < L.r[low].key):

high--


Project 4:

void 遍历一遍(SqList L)
{
    int low, high;
    L.r[0] = L.r[1];
    low = 1;
    high = L.length;
    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++;
    }
}

Project 5:

Also, we are missing one last step in the program algorithm: putting the sentinel back in the middle

void 遍历一遍(SqList L)
{
    int low, high;
    L.r[0] = L.r[1];
    low = 1;
    high = L.length;
    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];
}

OK, in fact, I still reserved a small easter egg here for everyone, if you are interested, you can take a look at the next diary

Haha, in fact, you have to read it, you have to read it, because although the sentence we wrote here is correct, it is not completely correct, haha

Guess you like

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