The road to learning sorting algorithms - table insertion sort
Table insertion sort was briefly mentioned in Insertion sort (concept) . I briefly summarized it and wrote this article. You can refer to it if you need it.
Table insertion sort, as the name implies, uses an index table to sort the original table. The advantage of this is that it saves the process of moving the elements in the original table. Of course, it is also convenient to move elements in a single integer array (only for experimental use), but for a table with a complex structure, it is really not an easy task to move elements in the table. For example (the following two-dimensional array in PHP)
$arr = array(
1=>array("uname"=>'张三','age'=>20,'occu'=>'PHP程序员'),
2=>array("uname"=>'李四','age'=>27,'occu'=>'PHP程序员'),
3=>array("uname"=>'赵五','age'=>19,'occu'=>'PHP程序员'),
4=>array("uname"=>'王六','age'=>33,'occu'=>'PHP程序员'),
5=>array("uname"=>'刘大','age'=>35,'occu'=>'PHP程序员'),
6=>array("uname"=>'公子纠','age'=>29,'occu'=>'PHP程序员'),
7=>array("uname"=>'公子小白','age'=>26,'occu'=>'PHP程序员'),
8=>array("uname"=>'管仲','age'=>80,'occu'=>'PHP程序员'),
9=>array("uname"=>'孔丘','age'=>76,'occu'=>'PHP程序员'),
10=>array("uname"=>'曾子','age'=>66,'occu'=>'PHP程序员'),
11=>array("uname"=>'子思','age'=>55,'occu'=>'PHP程序员'),
12=>array("uname"=>'左丘明','age'=>32,'occu'=>'PHP程序员'),
13=>array("uname"=>'孟子','age'=>75,'occu'=>'PHP程序员'),
14=>array("uname"=>'宋襄公','age'=>81,'occu'=>'PHP程序员'),
15=>array("uname"=>'秦穆公','age'=>22,'occu'=>'PHP程序员'),
16=>array("uname"=>'楚庄王','age'=>45,'occu'=>'PHP程序员'),
17=>array("uname"=>'赵盾','age'=>58,'occu'=>'PHP程序员'),
18=>array("uname"=>'廉颇','age'=>18,'occu'=>'PHP程序员'),
19=>array("uname"=>'蔺相如','age'=>39,'occu'=>'PHP程序员'),
20=>array("uname"=>'老子','age'=>100,'occu'=>'PHP程序员'),
);
For this array, if we only want to sort the age, but do not want to change the position of each element, we can use table insertion sort. Sort the current table with the help of an index table.
Okay, let's start to analyze the table insertion sort. First we need an index table, the table structure is as follows (still using PHP as an example)
array(
index=>array('next'=>值)
)
index is the index of the element in the original table
next points to the next index
For example, the following elements need to be sorted
We assume that the subscript starts from 1, with 0 as the starting index. Then after sorting, the index table (called table B) is as follows
Next, we construct this index table step by step according to this example
Step 1: Initialize the index table and set its two elements
Step 2: Traverse table A. The current value of the second element in table A is 5. Then start from the next value at position 0 in the index table (hereinafter referred to as table B), and compare A[$next] and 5 in turn. There are two conditions for terminating the traversal of table B: $next is 0 and A[$next] is greater than or equal to 5.
A[1] is greater than 5, so the next value of B[0] is changed to 2 and the next value of B[2] is 1 as follows
$next = B[0][next] 开始为1
while($next 不等于0){
if(A[$next]<5)
$next = B[$next][next] 此时$next值为0
If(A[$next]>=5)
跳出循环
}
If($next等于0 ) //说明直到B表中的最后一个元素仍然没有比5大的元素 则将B[2]的next 值置为0,B[1]的next值置为2 其它的不变
If($next 不等于0) //说明A[$next]值大于等于 5 则将 B[0][next] 置为2 ,B[2][next]置为1
The third step is to traverse table A. The current value of the third element in table A is 9. The steps are the same as the second step and will not be repeated here. After the third step, tables A and B are as follows
B[3][next] = B[2][next]
B[2][next] = 3
Step 4: Same as step 2. Tables A and B are as follows
B[4][next] = B[2][next]
B[2][next] = 4
So far, the construction method of the index table has been introduced. I don't know if I have explained it clearly to you. If you have any questions, you can leave a message below and I will reply as soon as I see it.
Next, we use PHP to implement table insertion sorting test data using the two-dimensional array at the beginning of the article.
$link = array(); //链表
$link[0]=array('next'=>1);//初始化链表 $link第一个元素仅仅作为头部
$link[1]=array('next'=>0); //将第一个元素放入$link
/*
* 开始遍历数组 从第二个元素开始
*/
for($i=2;$i<=count($arr);$i++){
$p = $arr[$i]; //存储当前待排序的元素
$index =0;
$next = 1; //从开始位置查找链表
while($next!=0){
if($arr[$next]['age']<$p['age']){
$index = $next;
$next = $link[$next]['next'];
}
else break;
}
if($next == 0){
$link[$i]['next'] = 0;
$link[$index]['next'] = $i;
}else{
$link[$i]['next']=$next;
$link[$index]['next']=$i;
}
}
At this point, the index table has been built. Just traverse this index table to see the effect we want. The following is the code for the output result
$next = $link[0]['next'];
while($next!=0){
print_r($arr[$next]);
$next = $link[$next]['next'];
}
From the above code, we can easily see that the time complexity of table insertion sort is still O(n²)
Well, the above is all the content about table insertion sort. You are welcome to leave a message below to discuss and improve together.
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