Detailed explanation of insertion sort (C language)

Preface
Insertion sort is a simple and intuitive sorting algorithm. Insertion sort performs very well when sorting small-scale data or partially ordered. Today I will teach you how to use insertion sort. let's go ! ! !
Insert image description here

insertion sort

The basic idea of ​​insertion sort is to divide the sequence to be sorted into two parts: sorted and unsorted. Initially, the first element is treated as the sorted sequence, and the remaining elements are treated as the unsorted part. Then insert the elements of the unsorted part into the correct position of the sorted sequence one by one until all elements are inserted into the sorted sequence.
For example:
This is an array, we want to sort it from small to large.
1 6 8 9 5 2 3
According to the idea just now, we consider 1 as the sorted part, and the others as the parts to be sorted. We need to insert the elements of the part to be sorted into the sorted part one by one. First, we insert 6 in After 1, because 6 > 1, now 6 is the sorted part, and the following 8 and 9 are also added to the back. The current sorted part is 1689. We need to insert 5 into it. First, compare 5 and 9. 5 < 9, then compare 5 and 8, 5 < 8, then compare 5 and 6, 5 < 6, then compare 5 and 1, 5 > 1, so 5 needs to be inserted between 1 and 6, the remaining The same goes for 2 and 3. After inserting, you can get the target sequence.
Let's implement it with code:

#include<stdio.h>
int main()
{
    
    
	int arr[6] = {
    
     1,1,4,5,1,4 };//创建数组
	for (int i = 1; i < 6; i++)
	{
    
    
		int end = i;//记录当前位置
		while (end)
		{
    
    
			if (arr[end - 1] > arr[end])//交换位置
			{
    
    
				int tem = arr[end];
				arr[end] = arr[end - 1];
				arr[end-1] = tem;
				end--;
			}
			else//已经插入退出循环
			{
    
    
				break;
			}
		}
	}
	return 0;
}

Optimization
We will find that the sorted part is monotonic, so can we use the dichotomy method? Using the bisection method can greatly speed up the efficiency of our insertion. After we find the position that needs to be inserted through binary search, we need to move the elements in the interval from this position to the recorded position by one space, and then insert this number, for example:
1 6 8 9 5.
We find 5 through binary search. Place it between 1 and 6, then we need to move 6-9 back one space 1 6 8 9 and then insert 5 into the empty space.
code show as below:

#include<stdio.h>
int efcz(int* arr, int right)//二分查找函数
{
    
    
	int end = right + 1;//目标数下标
	int left = 0;
	while (left <= right)
	{
    
    
		int mid = (left + right) / 2;
		if (arr[mid] >= arr[end])
		{
    
    
			right = mid - 1;
		}
		else
			left = mid + 1;
	}
	return left;
}
int main()
{
    
    
	int arr[6] = {
    
     1,3,4,5,6,2 };//创建数组
	for (int i = 1; i < 6; i++)
	{
    
    
		int right = i-1;//右边界
		int ret = efcz(arr, right);//二分查找插入位置
		int tem = arr[i];//临时变量
		for (int j = right; j >= ret; j--)//数组从ret到rihgt整体后移一格
		{
    
    
			arr[j + 1] = arr[j];
		}
		arr[ret] = tem;//插入
	}
	return 0;
}

end

Friends who see here must have mastered the use of insertion sort. If you think the blogger’s talk is good, can you give the blogger a free follow, like, and favorite to support it? The blogger will continue to share more knowledge, follow the blogger to not get lost~, see you in the next issue!
(By the way, today is Christmas, Merry Christmas my friends!)

Guess you like

Origin blog.csdn.net/Tokai___Teio/article/details/135203459