[Sorting] Direct insertion sorting and Hill sorting (detailed illustration)

introduction

In the previous article, we implemented selection sorting and heap sorting. In this article, we will continue to introduce direct insertion sorting and Hill sorting:

Both direct insertion sorting and Hill sorting belong to a type of insertion sorting:
the idea of ​​these two sortings is to take elements from the part to be sorted in turn and insert them into the appropriate position in the sorted part in front of it. Iterate so that the entire array is sorted.

direct insertion sort

train of thought

The idea of ​​direct insertion sorting is basically the same as the above idea:
that is, at the beginning, the part to be sorted is the entire array, from which elements are taken in turn, and inserted into the appropriate position in the ordered part in front of the element. Then iterate, the part to be sorted is decremented, and finally the entire array is sorted.

accomplish

This kind of thinking obviously requires two layers of loops:
the inner loop is a single-pass sorting: in each sorting, it is necessary to put the element at the starting position of the part to be sorted at the correct position in the front part of the element, that is, the element Compares with the preceding elements in turn, and places it after the first element that is smaller than it. Since the element is always placed in the correct position in the front each time it is inserted, the front part is always in order; the
outer loop requires several loops: control the number of elements to be sorted, that is, from the length of the array Start, decrement until zero. After the outer loop ends, the sorting is complete:

insert image description here

//直接插入排序
void InsertSort(int* a, int n)
{
    
    
	int i = 0;
	for (i = 1; i < n; i++)
	{
    
    
		int temp = a[i];
		int end = i - 1;
		while(end>=0)
		{
    
    
			if (temp < a[end])
			{
    
    
				a[end + 1] = a[end];
				end--;
			}
			else
			{
    
    
				break;
			}
		}
		a[end + 1] = temp;
	}
}

Hill sort

train of thought

Hill sorting is actually an optimization of direct insertion sorting:
combined with the previous direct insertion sorting, we found that
when the amount of sorted data is large, it takes a lot of time to move a small element from the back of the array to the front. The time will be much longer;
when a set of data is partially ordered, the first element of the part to be sorted can be inserted at the end of the ascending part, and the efficiency of direct insertion sorting will be much higher.

The difference from direct insertion sorting is that the array is divided into multiple parts with equal ranges, and each part has gap elements. By directly inserting and sorting the elements at the same position in each part, the elements behind the array can be quickly moved to the front of the array, that is, only the original n/gap times are needed to move, and a relatively ordered Arrays, again for efficiency:

accomplish

According to the above ideas, when implementing Hill sorting, the previous two-layer loop of direct insertion sorting only needs to be slightly modified, and the comparison with the previous element is changed to the comparison with the previous gap-th element, and then the two-layer loop is outsourced One layer of loop to control the value of gap is enough;
when confirming the value of gap, we can directly initialize the value of gap to the length of the array, and loop gap/=2 each time, and finally gap will be equal to 1. When the gap is 1, and then the insertion sort of the inner layer is performed, the sorting is a basically ordered array, and the efficiency will be very high:

insert image description here

 //希尔排序
void ShellSort(int* a, int n)
{
    
    
	int gap = n;
	while (gap >= 1)
	{
    
    
		int i = 0;
		for (i = gap; i < n; i++)
		{
    
    
			int temp = a[i];
			int end = i - gap;
			while (end >= 0)
			{
    
    
				if (temp < a[end])
				{
    
    
					a[end + gap] = a[end];
					end-=gap;
				}
				else
				{
    
    
					break;
				}
			}
			a[end + gap] = temp;
		}
		gap /= 2;
	}
}

Summarize

So far, the content about direct insertion sorting and Hill sorting has been introduced.
Next, we will continue to introduce other sorting. Welcome to continue to pay attention.

If you think that I did not introduce a certain part clearly or that there is a problem with a certain part, you are welcome to raise it in the comment area

If this article is helpful to you, I hope it will be connected with one click

Hope to make progress together with you

Guess you like

Origin blog.csdn.net/weixin_73450183/article/details/130209603