This time for everyone to bring PHP direct insert sort case analysis, PHP direct insertion sort of considerations, the following is the actual case, together to see.
Algorithm Introduction:
Poker is a game that we have played for almost everyone. Usually when we start the time is usually a person licensing, other people are on the side of the card, while the card, if you touch the first card is 5, the second card is 3, naturally we put 3 into the front of 5, the third card is 4, found in the middle of 3 and 5, the last card is 6, put on the back of 5, the fifth card is 2, Plug in front of 3; Finally, when we touch all the cards, the hands of the cards are from small to large (points) ordered.
Let's take a look at this order:
5 3//Insert 3 in an ordered table with only one element 5
3 5 4//Insert 4 in an ordered table with two elements 3 5
3 4 5 6//Insert 6 in an ordered table with two elements 3 4 5
3 4 5 6 2//Insert 2 in an ordered table with two elements 3 4 5 6
2 3 4) 5 6
Basic idea:
The basic idea of a direct insert is that each time the first element is taken out of the unordered table, it is inserted into the proper position of the ordered table, so that the ordered table remains orderly.
The first two numbers are compared, then the second number is inserted into the ordered table by size, and the second pass scans the third data with the first two numbers, inserts the third number into the ordered table by size, and then goes through the n-1 scan to complete the sorting process.
A direct insert sort consists of a two-layer nested loop. The outer loop identifies and determines the value to be compared. The inner loop determines its final position for the value to be compared. A direct insert sort compares the value to be compared to its previous value, so the outer loop starts with the second value. The current value continues to loop compared with a value larger than what is to be compared until it is found to be smaller than the value to compare and the value to be compared is placed in a subsequent position, ending the cycle.
The basic method for inserting a sort is to insert a record to be sorted by the size of its keyword into the appropriate position in the previously sorted sequence, each step until all records have been inserted.
Algorithm implementation:
<?php//directly into sort function swap (array & $arr, $a, $b) { $temp = $arr [$a]; $arr [$a] = $arr [$b]; $arr [$b] = $temp;} function Insertsort (array & $arr) { $count = count ($arr); The first element in the array is an existing ordered table for ($i = 1; $i < $count; $i + +) { $temp = $arr [$i]; Set Sentinel for ($j = $i-1; $j >= 0 && $arr [$j] > $temp; $j-) { $arr [$j + 1] = $arr [$j]; Record Move } $arr [$j + 1] = $temp; Insert to the correct position }} $arr = Array (9,1,5,8,3,7,4,6,2); Insertsort ($arr); Var_dump ($arr);
Operation Result:
Array (9) {[0]=> int (1) [1]=> int (2) [2]=> int (3) [3]=> int (4) [4]=> int (5) [5]=> int (6) [6]=> Int (7) [7]=> Int (8) [8]=> int (9)}
The time complexity of the direct insertion sorting algorithm is O (n^2).
A direct insert sort is a stable sort.
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP Quick Sort algorithm using steps
PHP Build Promotion Poster steps