Data Structure and Algorithm Basis (Wang Zhuo) (34): Bubble Sort of Exchange Sort

Table of contents

Preconditions:

Bubble sorting: (Under the requirements of the preconditions of our [previously sorted order table], I will not repeat them later)

first step:

For the first time/traversal, first determine the last/largest element

Step two:

Notice:

In addition, if you want to review how we learned this thing before, you can see:

third step:

Ideas:

method:

accomplish:


Preconditions:

#include<iostream>
using namespace std;

#define MAXSIZE 20  //记录最大个数
typedef int KeyType;  //关键字类型

typedef int InfoType;

//定义每个记录(数据元素)的结构
struct RecType
    //Record Type:每条记录的类型
{
    KeyType key;  //关键字
    InfoType otherinfo;  //其他数据项
};

struct SqList
    //顺序表(的)结构
{
    RecType r[MAXSIZE + 1];
    //类型为【记录类型】的数组
    //r[0]一般做哨兵或缓冲区
    int length;  //顺序表长度
};

Bubble sorting: (Under the requirements of the preconditions of our [previously sorted order table], I will not repeat them later)


first step:

For the first time/traversal, first determine the last/largest element

Write the (first) bubble sort program

void BubbleSort(SqList& L)
{
    for (int i = 1; i <= L.length; i++)
    {
        if (L.r[i].key > L.r[i + 1].key)
        {
            L.r[0] = L.r[i + 1];
            L.r[i + 1] = L.r[i];
            L.r[i] = L.r[0];
        }
    }
}

The default data location is stored from the position with the bit order of 1, and the position of 0 is vacated to store the sentinel/as a temporary storage space 


Step two:

Each subsequent loop determines the last element of the remaining elements

The remaining elements that can be sorted and compared are getting less and less, and each time the cycle is sorted: the
largest element in it is determined, and there is one less element that will be entered (brought) for sorting and comparison in the next round

So... / So:

void BubbleSort(SqList& L)
{
    for (int j = L.length; j > 0; j--)
        //j > 1 是不是也可以?
        //反正最后都只剩下一个元素了,肯定是最小的那一个,你还比什么比
        for (int i = 1; i < j; i++)
        {
            if (L.r[i].key > L.r[i + 1].key)
            {
                L.r[0] = L.r[i + 1];
                L.r[i + 1] = L.r[i];
                L.r[i] = L.r[0];
            }
        }
}

 j means:

How many elements do we compare each time (this time) loop

This cycle determines the number/largest element in the entire sorting table

And i represents/is used to point to the previous element that is used for comparison each time


Notice:

In addition to the previous step where we perfected the closed-loop design statement for multiple cycles, here we have another change that needs attention:

        for (int i = 1; i < j; i++)

Here, the symbol we use is "<" instead of the "<=" we used earlier

This change here can be said to be one of the essence of our bubble sorting algorithm

The reason is actually very simple:

Because the objects we compare are: Lr[i].key and Lr[i + 1].key

Not: Lr[i].key and Lr[i - 1].key

Of course, in fact, it’s not that there is any difference between the second step and the first step. In fact, it’s just that we also made a mistake in the first step, haha


In addition, if you want to review how we learned this thing before, you can see:

Book P85 Example 5-3 Bubble algorithm problem_i<k, Nuo a[i is less than a[k]_宇-Yu's Blog-CSDN Blog

But I personally don’t think it is necessary, because I feel that what I wrote here (written directly) is clear and comfortable

The previous article was written like a mess, haha


Anyway, about how to write this bubble sort, in fact, it is still different. Some of the versions are collected below, but I think as long as you know that I am right, then it will work:


Github version:

    for(int i=0;i<L.length;++i)
    {
        for(int j=0;j<L.length - i;++j)

PPT standard answer:

void BubbleSort(SqList& L) 
{
    int i, j, m;
    RecType temp; //交换时临时储存
    bool flag = 1;  //标记是否有交换
    for (m = 1; m < L.length - 1 && flag; m++) 
    {
        flag = 0;
        for (j = 1; j <= L.length - m; j++) 
        {
            if (L.r[j].key > L.r[j + 1].key) 
            {  
                flag = 1;
                temp = L.r[j];
                L.r[j] = L.r[j + 1];
                L.r[j + 1] = temp;
            }
        }
    }
}

In addition, as for the other writing methods I wrote in my previous article, I won’t repeat them here. Those who are interested can go and see for themselves.


third step:

Add to the improvement method mentioned above in PPT:

Ideas:

Because each sorting traverses the entire table organized by all the previous elements, then that is to say:

If we have a traversal, the previous elements are already in order, so we don’t need to operate and exchange positions

That's it, the whole table is in order

We don't need to repeat the traversal operation later

method:

Put up a flag to mark whether the previous elements are in order in the previous sorting, and if flag = 0, there is no need to traverse

accomplish:

Localize this thought process:

void BubbleSort(SqList& L)
{
    bool flag = 1;
    for (int j = L.length; j > 0 && flag; j--)
        for (int i = 1; i < j; i++)
        {
            flag = 0;
            if (L.r[i].key > L.r[i + 1].key)
            {
                flag = 1;
                L.r[0] = L.r[i + 1];
                L.r[i + 1] = L.r[i];
                L.r[i] = L.r[0];
            }
        }
}

Guess you like

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