[Classic sorting] Insertion and Hill sorting

Table of contents

foreword

1. Insertion sort

1.1 Introduction

1. 2 Implementation idea

1. 3 Code implementation

2. Hill sort

2. 1 Introduction

2. 2 Implementation ideas

2. 3 Code implementation

Write at the end:


foreword

There is order everywhere in life.

You go to the shopping website to filter items, sorted by time, price, etc.,

And sorting is also a knowledge point that is often tested in interviews.

Today, we start with insertion and Hill sorting to explore the mysteries of classic sorting.

1. Insertion sort

1.1 Introduction

Insertion sort, as the name suggests:

He is just like when we usually play poker,

From front to back, pick up the small number and insert it in front of the big number (here the default is ascending order)

Insertion sort is like this,

Let's not talk nonsense, let's talk about the idea of ​​realization:

1. 2 Implementation idea

To implement a sort, we need to know

The address of the first element of the array to be sorted, and the size of the array. (the basic)

The picture shows the idea of ​​inserting and arranging:

Iterate over the array:

Starting from the second number, compare all the way forward,

Use tmp to save your own value first,

(main idea)

If it is smaller than the previous number, move the previous number backwards,

Until it is larger than the previous number, the position vacated by moving backward will be occupied.

1. 3 Code implementation

Note: a represents the address of the first element of the array array, and n represents the size of the array.

//插入排序
InsertSort(int* a, int n) {

	//遍历数组
	for (int i = 1; i < n; i++) {

		//end位置表示要被比较的数
		int end = i - 1;

		//tmp存放的是正在进行插入排序的数
		int tmp = a[i];

		while (end >= 0) {
			//如果tmp比end位置的数小
			if (tmp < a[end]) { 
				//就让end位置的数往后挪动
				a[end + 1] = a[end];
				//再跟前一个end位置的数比较
				end--; 
			}
			else {
				//如果tmp比end位置的数大,那就别动了
				break; 
			}
		}
		//将之前因为往后挪动而空出的位置霸占
		a[end + 1] = tmp;
	}
}

2. Hill sort

2. 1 Introduction

Hill sort is based on insertion sort, which is why I talked about insertion sort first.

Insertion sort has a characteristic, that is, when the sorted array is closer to order,

The higher the efficiency of insertion sorting, the closer to O(N),

Hill sorting makes the array more and more close to order by means of pre-sorting.

Insertion sort is performed at the end.

2. 2 Implementation ideas

Presorting is done by dividing the array into parts and performing insertion sort on them:

For example:

We use gap equal to 3 to divide the array into three parts:

(Three different colors indicate those three parts)

They are:

9, 6, 3, 0

8, 5, 2

7, 4, 1

Perform insertion sort on them respectively, that is, pre-sort,

Each insertion can change from one-by-one comparison insertion to jumping comparison insertion, which greatly improves the efficiency.

So: (core idea)

By continuously performing efficient pre-sorting,

Finally, insertion sort is performed on the nearly ordered array,

To improve the efficiency of insertion sort, this is Hill sort.

Here is the implementation:

2. 3 Code implementation

//希尔排序
ShellSort(int* a, int n) {

	//将gap初始化成数组大小(之后再慢慢让它变小)
	int gap = n;

	//当gap == 1,证明已经完成最后一步:插入排序
	while (gap > 1) {

		//不断/2,让gap不断变小进行预排,当gap == 1时进行插入排序
		gap /= 2;

		//这个是将gap分成的每个部分进行插入排序,也就是预排序
		for (int i = 0; i < gap; i++) {
			for (int j = i; j < n - gap; j += gap) {

				//end位置表示要被比较的数
				int end = j;

				//tmp存放的是正在进行插入排序的数
				int tmp = a[end + gap];

				//插入排序
				while (end >= 0) {
					//从跳过一个数,变成跳过gap个
					if (tmp < a[end]) {
						a[end + gap] = a[end];
						end -= gap;
					}
					else {
						break;
					}
				}
				a[end + gap] = tmp;
			}
		}
	}
}

Write at the end:

The above is the content of this article, thank you for reading.

If you like this article, please like and comment, and write down your opinions.

If you want to learn programming with me, you might as well follow me, we will learn and grow together.

I will output more high-quality content in the future, welcome to watch.

Guess you like

Origin blog.csdn.net/Locky136/article/details/129894364