Home > Article > Backend Development > PHP direct insertion sort case analysis
This time I will bring you a case analysis of PHP direct insertion sorting. What are the precautions for PHP direct insertion sorting? . The following is a practical case, let's take a look.
Algorithm introduction:
Poker is a game that almost everyone of us has played. Usually when we start, one person deals the cards, and everyone else draws and sorts the cards. If the first card you draw is 5 and the second card is 3, naturally we insert the 3. Go to the front of 5; the third card is 4, find it between 3 and 5; the fourth card is 6, put it behind 5; the fifth card is 2, insert it in front of 3;…. Finally, when we have drawn all the cards, the cards in our hands are sorted from small to large (points).
Let’s look at this sequence:
5 3 3 // Insert 3 into an ordered list with only one element 5
3 5 4 4 // Insert 3 into an ordered list with only one element 5 Insert 6 into an ordered list with two elements 3 5
3 4 5 6 6 // Insert 6 into an ordered list with two elements 3 4 5
3 4 5 6 2 2 In an ordered list of two elements 3 4 5 6
2 3 4 5 6
Basic idea:
The basic idea of direct insertion sort is: take out the first element from the unordered list each time and insert it into the appropriate position of the ordered list, so that the ordered list remains in order.
The first pass compares the first two numbers, and then inserts the second number into the ordered list according to size; the second pass scans the third data and the first two numbers from back to front, and The third number is inserted into the ordered list according to size; this is continued in sequence, and the entire sorting process is completed after (n-1) scans.
Direct insertion sort is composed of two levels of nested loops. The outer loop identifies and determines the values to be compared. The inner loop determines the final position of the values to be compared. Direct insertion sort compares the value to be compared with its previous value, so the outer loop starts from the second value. If the current value is larger than the value to be compared, the loop comparison continues until a value smaller than the value to be compared is found and the value to be compared is placed in the next position, ending the cycle.
The basic method of insertion sort is: at each step, a record to be sorted is inserted into the appropriate position in the previously sorted sequence according to the size of its key, until all records are inserted.
Algorithm implementation:
<?php //直接插入排序 function swap(array &$arr,$a,$b){ $temp = $arr[$a]; $arr[$a] = $arr[$b]; $arr[$b] = $temp; } function InsertSort(array &$arr){ $count = count($arr); //数组中第一个元素作为一个已经存在的有序表 for($i = 1;$i < $count;$i ++){ $temp = $arr[$i]; //设置哨兵 for($j = $i - 1;$j >= 0 && $arr[$j] > $temp;$j --){ $arr[$j + 1] = $arr[$j]; //记录后移 } $arr[$j + 1] = $temp; //插入到正确的位置 } } $arr = array(9,1,5,8,3,7,4,6,2); InsertSort($arr); var_dump($arr);
Running 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).
Direct insertion sort is a stable sort.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the steps to use PHP quick sorting algorithm
Detailed explanation of the steps to generate promotion posters in PHP
The above is the detailed content of PHP direct insertion sort case analysis. For more information, please follow other related articles on the PHP Chinese website!