data structure insertion sort

Insert image description here
Long time no see. I've been busy these days. I haven't studied for almost a week and I'm almost unfamiliar with the keyboard. I just learned a little bit about sorting today. I'll also update an insertion sort for you. I will gradually sort the eight majors later. After the update, there is also the binary tree. The editor of the binary tree has limited abilities and may have learned a little bit. Without further ado, let’s get into today’s study.

Insertion sort is somewhat similar to the bubble sort we learned before. First of all, their space complexity is O(N*N). What is the whole process of insertion sort? First, let’s introduce a small problem. , that is, we insert a number into an ascending sequence array. How do we insert it? First, we should compare this number, starting from the end. If it is larger than this number, we will continue to search forward. , until we find a number greater than or equal to this number, we insert it at the end of the number. During the insertion process, if this number is smaller than other numbers, the other numbers should be moved backward, which is equivalent to an overlay. As for the idea, we will give you a picture here to make it easier for everyone to understand.

Insert image description here
Insert image description here
We end keep walking forward until we find a suitable position. There is another process in the middle, which is covering. It is not reflected in the drawing. It means moving backward to cover if the conditions are not met. It may be better to understand it by looking at the code. .

	int end = 9;
		int tmp = a[end + 1];
		while (end >= 0)
		{
    
    
			if (tmp < a[end])
			{
    
    
				a[end + 1] = a[end];
				end--;
			}
			else
			{
    
    
				break;
			}
		}
		a[end+1] = tmp;
	}

Suppose we have nine numbers. Now we need to put 1 into this array. How do we change it? The above code can reflect it. So after we have this idea, can we simulate insertion sorting? In fact, it is very easy. It's as simple as putting a loop outside me. Let's give it a try.

void InsertSort(int* a, int n)
{
    
    
	int i = 0;
	for (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];
				end--;
			}
			else
			{
    
    
				break;
			}
		}
		a[end+1] = tmp;
	}
}

In this way, our insertion sort can be implemented. We can also test
the header file

InsertSort.h


#include<stdio.h>

void InsertSort(int* a, int n);

void PrintInsert(int* a, int n);

InsertSort.c

#include"Insert.h"

void InsertSort(int* a, int n)
{
    
    
	int i = 0;
	for (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];
				end--;
			}
			else
			{
    
    
				break;
			}
		}
		a[end+1] = tmp;
	}
}

void PrintInsert(int* a, int n)
{
    
    
	int i = 0;
	for (i = 0; i < n; i++)
	{
    
    
		printf("%d ", a[i]);
	}
	printf("\n");
}

Test.c

int main()
{
    
    
	int arr[] = {
    
     9,8,7,6,5,4,3,2,1 };
	InsertSort(arr, sizeof(arr) / sizeof(int));
	PrintInsert(arr, sizeof(arr) / sizeof(int));
	return 0;
}

After debugging, you can turn it into order. Today we will talk about a sort. Because the editor is lazy, I only learned one sort today hahaha

See you next time

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/132698989