[Algorithm] Sorting - Insertion Sort and Hill Sort

Table of contents

Preface

1. The concept of sorting and its application

1.1 Concept of sorting

1.2 Application of sorting

1.3 Common sorting algorithms

2. Implementation of insertion sort 

Optimization based on insertion sort - Hill sort (shrinking incremental sort)


 

 =========================================================================

Homepage

code repository

C language column

Elementary data structure column

Linux column

LeetCode brush questions 

Algorithm column 

=========================================================================

Preface

This is a newly opened algorithm column. As we all know, algorithms are relatively difficult and very brain-consuming. In the past few issues, we will introduce some simple sorting algorithms to help everyone get used to them. Today’s first issue brings you insertion sorting and Optimized Hill sort for insertion sort.


1. The concept of sorting and its application

Sorting can be seen everywhere in life, such as grades, company rankings, university rankings, and store rankings on some food software, etc. They are all sorting, so what is sorting? How to implement sorting? Please listen to me carefully.

1.1 Concept of sorting

Sorting: The so-called sorting is the operation of arranging a string of records in increasing or decreasing order according to the size of one or some keywords in it.
Stability: Assume that there are multiple records with the same keyword in the record sequence to be sorted. If sorted, the relative order of these records remains unchanged, that is, in the original sequence, r[i]=r[j] , and r[i] is before r[j], and in the sorted sequence, r[i] is still before r[j], then this sorting algorithm is called stable; otherwise it is called unstable.
Internal sorting: sorting in which all data elements are placed in memory.
External sorting: There are too many data elements that cannot be placed in the memory at the same time. According to the requirements of the sorting process, the data cannot be moved between internal and external memory.

1.2 Application of sorting

 

1.3 Common sorting algorithms

 


2. Implementation of insertion sort 

Basic idea: Insert the records to be sorted into an already sorted ordered sequence one by one according to the size of their key values, until all records are inserted, and a new ordered sequence is obtained.

Insertion sort in life

Friends who like card games must be familiar with poker and mahjong! When we organize the cards, we use insertion sorting

When inserting the i (i>=1) element, the previous array[0], array[1],...,array[i-1] have been sorted. At this time, use the sorting code of array[i] and The sorting code order of array[i-1], array[i-2],... is compared. When the insertion position is found, array[i] is inserted, and the order of the elements at the original position is moved backward.

To put it simply : the last valid data in the array is the data we want to insert. It is compared with the previously sorted data in sequence and finally placed in the appropriate position.

Summary of the characteristics of direct insertion sort:
1. The closer the element set is to order, the higher the time efficiency of the direct insertion sort algorithm
2. Time complexity: O(N^2)
3. Space complexity: O(1) , which is A stable sorting algorithm
4. Stability: Stable 

Implement code

void InsertSort(int* a, int n)
{
	//[0,end]有序,把end+1位置的插入到前序序列
    //控制[0,end+1]有序
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = a[end + 1];
		while (end >= 0)
		{
			if (tmp < a[end])
			{
				a[end + 1] = a[end];
			}
			else
			{
				break;
			}
			end--;
		}
		a[end + 1] = tmp;
	}
}

Optimization based on insertion sort - Hill sort (shrinking incremental sort)

Hill sorting method is also called shrinking increment method . The basic idea of ​​Hill sorting methodis: first select an integer, divide all the records in the file to be sorted into groups, put all the records with distance into the same group, and sort the records in each group. Then, take and repeat the above grouping and sorting work. When =1 is reached, all records are sorted in the same group.

Steps of Hill sort:

1. Select an integer smaller than the length of the array (denoted as: gap) to group, and then use insertion sort to pre-sort each group.

2. As the program proceeds, the gap gradually decreases. When the gap is equal to 1, it is insertion sort.

Implement code

while (gap > 1)
	{
		gap = gap / 2;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (tmp < a[end])
				{
					a[end + gap] = a[end];
					end = end - gap;
				}
				else

				{
					break;
				}
				a[end + gap] = tmp;
			}
		}
	}

 Summary of the characteristics of Hill sorting:
1. Hill sorting is an optimization of direct insertion sorting.
2. When gap > 1, it is pre-sorted to make the array closer to order. When gap == 1, the array is already nearly ordered, so it will be fast. In this way, overall optimization results can be achieved. After we implement it, we can compare the performance tests.
3. The time complexity of Hill sorting is difficult to calculate because there are many ways to value gaps, which makes it difficult to calculate. Therefore, the time complexity of Hill sorting given in many trees is not fixed:

 "Data Structure (C Language Version)" --- Yan Weimin

"Data Structure - Description Using Object-Oriented Methods and C++" --- Yin Renkun

 Because our gap is calculated according to the method proposed by Knuth, and Knuth has conducted a large number of experimental statistics, we will temporarily calculate it according to: O (n^1.25) to O (1.6*n^1.25).

The meaning of Hill sorting:  Large numbers go to the back faster, and small numbers go to the front faster. The larger the gap, the faster the jump. The smaller the gap, the closer it is to insertion sort. When gap==1, it is insertion sort.

That’s it for today’s insertion sorting. I’ll bring you selection sorting in the next issue, so stay tuned! ! !

Guess you like

Origin blog.csdn.net/qq_55119554/article/details/133254740