Learning the Sorting Algorithm - Insertion Sort (Concepts)
What is "insertion sort"? The concept is as follows: each time a record to be sorted is inserted into the previously sorted sequence according to its key size, until all records are inserted.
Concepts are always somewhat abstract, and can also be called basic ideas. The concept of insertion sort mentioned above can also be said to be the basic idea of insertion sort. Abstract things are always difficult to understand, so what is needed here is to concretize the abstract concepts.
Let's convert it into a team problem: There will be two teams at the beginning, one of which is arranged in order from low to high, named Team A. The other team is disordered, named Team B. As shown below:
Now put the first person of Team B (let's call him Jack) into Team A, and keep Team A in order. To keep Team A in order, we need to find a suitable position for Jack in Team A. The person in front of this position should be shorter than Jack, and the person behind this position should be taller than Jack. Now we can let Jack stand in this position, and Team A is still in order.
Then put the second person of team B (call him Tom) into team A in the same way as jack, to ensure that team A remains in order.
This continues until all the people in Team B join Team A. At this point, the two teams have become one, and this team is in order.
After seeing this, you should have a clearer understanding of insertion sort. But there is still a question here. The sorting problem is to sort a queue, so where are the two teams? Let's change it again. At the beginning, teams A and B stand in the same row, and team A is in front of team B as a whole, and team A is one person. For one person, the queue must be orderly.
Then, let's get closer to the code and map teams A and B into arrays.
3 represents the initial team A, 5, 2, etc. represent the initial team B. 5 is Jack, and 2 is Tom.
When I was learning insertion sort, I understood it according to this idea. At this point, I have basically mastered the idea of insertion sort.
Different ways of converting thoughts into codes will also produce different "factions". For example, the "Spring and Autumn Annals" is always difficult to read, so someone came out to write a commentary on the Spring and Autumn Annals. Different people wrote different comments, such as "Zuo Zhuan", "Gongyang Zhuan", and "Guliang Zhuan". Although they are different, they all inherit the thoughts of the "Spring and Autumn Annals".
Now let's go back to our insertion sort. Now that the ideas are clear, the only difference is the implementation method. The key point is still Team A. When Team A is looking for the right position for Team B, there are many ways to find it.
1. Starting from the beginning each time to traverse and find the position is called direct insertion sort
2. Using the binary search method to find the position is called binary insertion sort/half insertion sort
The above two methods are to search and move elements in a queue, and the main time is spent on searching and moving.
There is also a third method, which is to use an additional queue to do a mapping, which can save the time spent on moving.
This method is called:
3. Table insertion sort
This involves two concepts: time complexity and space complexity. I will not explain these two concepts in this article. I will explain how I understand these two concepts in other blog posts.
Due to space constraints, this chapter does not involve any code. I will only share my understanding of insertion sort. In other articles later, I will provide you with the implementation code of each method I wrote.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of