JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

The road to learning sorting algorithms - table insertion sort

Author:JIYIK Last Updated:2025/03/19 Views:

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

Trace memory guest - table insertion sort diagram 1

For example, the following elements need to be sorted

Trace memory guest - table insertion sort Figure 2

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

Trace memory guest - table insertion sort Figure 3

Next, we construct this index table step by step according to this example

Step 1: Initialize the index table and set its two elements

Trace memory guest - table insertion sort Figure 4

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

Trace memory guest - table insertion sort Figure 5

$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

Trace memory guest - table insertion sort Figure 6

Step 4: Same as step 2. Tables A and B are as follows

B[4][next] = B[2][next]

B[2][next] = 4

Trace memory guest - table insertion sort Figure 7

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.

Article URL:

Related Articles

The road to learning sorting algorithms - Hill sort

Publish Date:2025/03/19 Views:50 Category:ALGORITHM

Hill sort is named after the designer of the algorithm, Hill. It is an improvement of Hill on the basis of insertion sort and can be said to be a special insertion sort. Here are the properties of insertion sort: First of all, the insertion

The road to learning sorting algorithms - merge sort

Publish Date:2025/03/19 Views:157 Category:ALGORITHM

Let's first look at the definition of merge sort Merge sort is an effective sorting algorithm based on the merge operation. This algorithm is a very typical application of the Divide and Conquer method. Merge the ordered subsequences to obt

The road to learning sorting algorithms - quick sort

Publish Date:2025/03/19 Views:121 Category:ALGORITHM

Quick sort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires O(n log n) comparisons. In the worst case, O(n2) comparisons are required, but this is uncommon. In fact, quick sort is often significantly faste

The road to learning sorting algorithms - heap sort

Publish Date:2025/03/19 Views:145 Category:ALGORITHM

Like other sorting algorithms, let's first look at the definition of heap sort. Heapsort : refers to a sorting algorithm designed using the heap data structure. A heap is a structure that approximates a complete binary tree and satisfies th

Common sorting algorithms

Publish Date:2025/03/19 Views:66 Category:ALGORITHM

This article introduces several commonly used sorting algorithms in software engineering. The core codes of all sorting algorithms are introduced in "Core Codes of Common Sorting Algorithms" 1. Insertion Sort The basic idea of ​​inserti

The road to learning sorting algorithms - selection sort

Publish Date:2025/03/19 Views:160 Category:ALGORITHM

Selection sort is a simple and intuitive sorting algorithm. Its basic idea is to select a maximum (or minimum) element in an unsorted sequence and put it at the end (note: this is the end of the unsorted sequence, which can be considered as

The road to learning sorting algorithms - bubble sort

Publish Date:2025/03/19 Views:80 Category:ALGORITHM

Bubble sort is also a simple and intuitive sorting algorithm. The idea is that it repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The work of visiting the sequence

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial