The path to learning by sorting algorithms -- table insertion and sorting -- Zhiyi blog

Source: Internet
Author: User
The path to sorting algorithm learning-table insertion and sorting- Yi blog briefly mentions table insertion and sorting in insert sorting (concept. I briefly summarized this article and wrote it for reference if necessary.

Insert sorting: As the name implies, an index table is used to insert and sort the original table. This saves the trouble of moving elements in the original table. Of course, it is quite convenient to move a single integer array (used only for testing), but for tables with complex structures, it is really not easy to move the elements in a table. For example (the following two-dimensional array in PHP)

$ Arr =Array(

1 =>Array("Uname" => 'Zhang San', 'age' => 20, 'occu '=> 'php programmer '),

2 =>Array("Uname" => 'Lily', 'age' => 27, 'occu '=> 'php programmer '),

3 =>Array("Uname" => 'Zhao 5', 'age' => 19, 'occu '=> 'php programmer '),

4 =>Array("Uname" => 'Wang Liu', 'age' => 33, 'occu '=> 'php programmer '),

5 =>Array("Uname" => 'Liu Da', 'age' => 35, 'occu '=> 'php programmer '),

6 =>Array("Uname" => 'canonicalization', 'age' => 29, 'occu '=> 'php programmer '),

7 =>Array("Uname" => 'small White ', 'age' => 26, 'occu' => 'php programmer '),

8 =>Array("Uname" => 'Guan Zhong', 'age' => 80, 'occu '=> 'php programmer '),

9 =>Array("Uname" => 'kongqiu ', 'age' => 76, 'occu' => 'php programmer '),

10 =>Array("Uname" => 'Zeng Zi', 'age' => 66, 'occu '=> 'php programmer '),

11 =>Array("Uname" => 'zs', 'age' => 55, 'occu '=> 'php programmer '),

12 =>Array("Uname" => 'Zuo Qiu Ming ', 'age' => 32, 'occu' => 'php programmer '),

13 =>Array("Uname" => 'mencius ', 'age' => 75, 'occu' => 'php programmer '),

14 =>Array("Uname" => 'Song Xianggong ', 'age' => 81, 'occu' => 'php programmer '),

15 =>Array("Uname" => 'Qin Mugong ', 'age' => 22, 'occu' => 'php programmer '),

16 =>Array("Uname" => 'Chu Zhuang Wang ', 'age' => 45, 'occu' => 'php programmer '),

17 =>Array("Uname" => 'Zhao yundun ', 'age' => 58, 'occu' => 'php programmer '),

18 =>Array("Uname" => 'Lian phe', 'age' => 18, 'occu '=> 'php programmer '),

19 =>Array("Uname" => 'Photo is like ', 'age' => 39, 'occu' => 'php programmer '),

20 =>Array("Uname" => 'Lao Tzu ', 'age' => 100, 'occu' => 'php programmer '),

);

For this array, if we only sort the age, but do not want to change the position of each element, we can use table Insertion sorting. Sort the current table with an index.

Now let's start to sort the table inserts for analysis. First, we need an index table. the table structure is as follows (taking php as an example)

Array (

Index =>Array('Next' => value)

)

Index indicates that the index next to an element in the original table points to its next index.

For example, the following elements need to be sorted:

For the moment, we think that its subscript starts from 1, and 0 digits are used as the start index. After sorting, the index table (called Table B) is as follows:

Next we will construct this index table step by step based on this example.

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

Step 2: traverse table A. Currently, the value of 2nd elements in Table A is 5. Then, starting from the next value of 0 bits in the index table (Table B in the future), we will compare the values of A [$ next] and 5 in sequence to terminate traversal of Table B. There are two conditions, first, $ next is 0. second, A [$ next] must be greater than or equal to 5.

The value of next in Table B [0] is 2 B [2] and the value of next in Table B is 1.

$ Next = B [0] [next] start with 1

While ($ next is not equal to 0 ){

If (A [$ next] <5)

$ Next = B [$ next] [next] at this time, the value of $ next is 0.

If (A [$ next]> = 5)

Skip loop

}

If ($ next equals 0) // description until the last element in Table B is not greater than 5, the next value of B [2] is set to 0, the next value of B [1] is set to 2.

If ($ next is not equal to 0) // If the value of A [$ next] is greater than or equal to 5, set B [0] [next] to 2, B [2] [next] is set to 1

Step 3: traverse table A. the value of 3rd elements in Table A is 9. The steps are the same as those in the second step. After step 3, table A and Table B are as follows:

B [3] [next] = B [2] [next]

B [2] [next] = 3

Step 4: same as step 2, Table A and Table B are as follows:

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

B [2] [next] = 4

Now, the index table construction method has been introduced. I don't know if I want to explain it clearly. if I have any questions, I can leave a message below. I will reply immediately after seeing it.

Next we will use PHP to insert table sorting test data and use the two-dimensional array at the beginning of the article.

$ Link =Array(); // Linked list

$ Link [0] =Array('Next' => 1); // initialize the first element of the linked list $ link as the header.

$ Link [1] =Array('Next' => 0); // add the first element to $ link

/*

* Start to traverse the array from the second element.

*/

For ($ I = 2; $ I <= count ($ arr); $ I ++ ){

$ P = $ arr [$ I]; // stores the elements to be sorted.

$ Index = 0;

$ Next = 1; // search for the linked list from the starting position

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;

}

}

Now that the index table has been built, you only need to traverse the index table to see what we want. The following is the output code.

$ Next = $ link [0] ['Next'];

While ($ next! = 0 ){

Print_r ($ arr [$ next]);

$ Next = $ link [$ next] ['Next'];

}

From the code above, we can easily see that the time complexity of table Insertion sorting is still O (n²)

Well, the above is the content of Insertion sorting for all tables. You are welcome to leave a message below to discuss and improve it together.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.