This article mainly introduces the PHP sorting algorithm Straight Insertion Sort (Straight Insertion Sort). It analyzes the principles and implementation techniques of the Straight Insertion Sort algorithm in detail in the form of examples. Friends in need can refer to it
The example in this article describes the PHP sorting algorithm Straight Insertion Sort. Share it with everyone for your reference, the details are as follows:
Algorithm introduction:
Here we still use one from "Dahua Data Structure" Example:
Poker is a game that almost all of us have 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 ordered.
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 sort algorithm is O(n^2).
Direct insertion sort is a stable sort.
This article is referenced from "Dahua Data Structure". It is only recorded here for future reference. Please don't criticize!
Related recommendations:
PHP sorting algorithm Hill Sort (Shell Sort)
##
The above is the detailed content of PHP sorting algorithm Straight Insertion Sort. For more information, please follow other related articles on the PHP Chinese website!