Home  >  Article  >  Backend Development  >  PHP sorting: Algorithmic idea and algorithm implementation of PHP insertion sort

PHP sorting: Algorithmic idea and algorithm implementation of PHP insertion sort

不言
不言Original
2018-08-14 16:11:241509browse

The content of this article is about PHP sorting: the algorithm idea and algorithm implementation of PHP insertion sort. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Algorithm introduction:

Here we still use an example from "Dahua Data Structure":

Poker is something that almost everyone of us has played. game. 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                                                                                                                                                                                                                         to 5 In the order of 5,
3 4 5 6 // Insert 6 into the order of two elements 3 4 5,
3 4 5 6 2 // Insert 2 Entering two elements 3 4 5 5 In the ordered list of 6
2 3 4 5 6

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 in the ordered list. position 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.

Insertion sort algorithm implementation:


// 直接插入排序
function swap(&$arr,$a,$b)
{
$temp    = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $temp;
}
function insertSort(&$arr)
{
$count = count($arr);
for ($i=1; $i = 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)
}

PHP sorting: Algorithmic idea and algorithm implementation of PHP insertion sort

Related recommendations:

php array sorting method sharing (bubble sort, selection sort)

## PHP implements heap sorting, PHP heap sorting_PHP tutorial

The above is the detailed content of PHP sorting: Algorithmic idea and algorithm implementation of PHP insertion sort. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn