Data Structure and Algorithm Fundamentals (Wang Zhuo) (30): Sorting Overview (Classification), Direct Insertion Sorting Ideas

Table of contents

Sorting and classification: (contents of this chapter)

By data storage medium: (learning content)

Internal sort:

External sort:

According to the number of comparators: (learning content)

Serial sort:

Parallel sort:

According to the main operation: (the learning content and the sorting inside will be focused on learning)

Compare sort:

Radix sort:

By auxiliary space:

Sort in place:

Sort out of place:

By stability:

Stable sort:

 Unstable sort:

By nature:

insertion sort

Preconditions:

1. Direct insertion sort

The train of thought is as follows:


Sorting and classification: (contents of this chapter)


By data storage medium: (learning content)

Internal sort:

The amount of data is small, the data is in the memory, and there is no need to exchange data between internal and external memory

External sort:

Large amount of data, data in external storage (file sorting)

When sorting external memory, the data needs to be loaded into the memory in batches for sorting, and the intermediate results must be put into the external memory in time, which is more complicated

Data does not fit in memory:

Sort a part of the memory, and put the data into the external memory after sorting

Take another part from the external storage, and then perform (new round) sorting


According to the number of comparators: (learning content)

Serial sort:

Single processor (comparing a pair of elements at the same time)

Parallel sort:

Multiprocessor (comparing multiple pairs of elements at the same time)


According to the main operation: (the learning content and the sorting inside will be focused on learning)

Compare sort:

insertion sort, swap sort, selection sort, merge sort

Radix sort:

Do not compare the size of the element, only determine its ordered position according to the value of the element itself


By auxiliary space:

Sort in place:

The amount of auxiliary space is O(1), which has nothing to do with the size of the data to be sorted

Sort out of place:

The amount of auxiliary space exceeds O(1), which is related to the size of the data to be sorted


By stability:

Stable sort:

Elements that can make any value equal, the relative order remains unchanged after sorting

 Unstable sort:

Elements with equal values ​​will change in relative order after sorting


By nature:

Natural sort:

The more ordered the input data, the faster the sorting speed

Unnatural ordering:

The more ordered the input data, the slower the sorting speed


insertion sort


Preconditions:

Storage structure: stored in a sequential table

#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;  //顺序表长度
};

int main()
{

}

1. Direct insertion sort

According to the PPT, sort out

The train of thought is as follows:

Of course, the "16" here is actually just a virtual finger, which points to (represents) all the numbers (objects) we use to compare with 7 


According to what is shown on page 23 of the PPT, it is obvious that:

The letter i is used to represent: the tail (sequence tail) of the ordered list (sequence table) before each cycle

Use the letter j to represent (complete) the entire process/operation of "insert"

Then (again) according to the 27th reminder of PPT, and the lessons learned based on our previous search algorithm:

We need to use sentry here


Finally, the basic framework of the program is sorted out as follows:

Supplement: add sentinel

For many elements to be inserted:
set up:

Pointer i points to the tail of the sequential/ordered sequence;
pointer j points to the new element:

Note:

The originally designed algorithm did not take into account the convenience that we can use the sentinel;
now, we no longer need to set up a pointer to the newly inserted element, just put the newly inserted element into the sentinel

So now, we are sure:

  • Put the newly inserted element into the sentinel (position with bit order 0)
  •  j points to (the position of) the element we are comparing with the newly inserted element in the sorted sequence
  • Compare the sentinel (bit order 0) element with the element pointed to by pointer j
  1. If greater than or equal to: (the sentinel element is greater than or equal to the element pointed to by pointer j)
    【Exchange elements】

    Move all the elements of the ordered sequence after j and j to the back by one bit

    (Because the new element to be inserted has already been placed in the sentinel, we don’t have to worry about whether there will be data loss if we go back one bit)

    Insert the sentinel element at the position pointed to by pointer j

    NOTE: Don't forget (i++)! ! !
     

  2. If less than: (the sentinel element is less than the element pointed to by pointer j)
    [Continue to compare, compare with the previous one]

    j--;

  3. i++

Guess you like

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