Home >Backend Development >PHP Tutorial >Principle and application of insertion sorting method in php

Principle and application of insertion sorting method in php

墨辰丷
墨辰丷Original
2018-06-13 11:17:561813browse

This article mainly introduces the PHP insertion sort method to implement array sorting. It analyzes the principles and specific implementation techniques of the insertion sort method with examples. It has certain reference value. Friends in need can refer to this article.

The example describes how to implement array sorting using PHP insertion sort.

The specific analysis is as follows:

The basic idea of ​​insertion sorting method: also use a case to illustrate, still use $arr = array(2,6,3,9), sort from large to small .

Implementation principle: Assume (not actually created) that there is an ordered array $arr = array(2), use $arr[1]=6 to compare with it, if 6>2, by $arr[0] is moved back to the $arr[1] position, and 6 is inserted into the $arr[0] position. Then, $arr[2]=3 is compared with $arr[1]=2, 3>2, then $arr[1]=2 continues to move to the $arr[2] position, the original $arr[3]=3 Insert it into the $arr[1] position, and then continue to insert 9 into the closing position. The final result is (9,6,3,2). Sorting can also be achieved after count($arr)-1 large loops.

Code pattern analysis:

The first big loop: $[1] and $[0] ratio;
The second big loop: $[2] and $[1 ] ratio, $[1] and $[0] ratio;
The third big cycle: $[3] and $[2] ratio, $[2] and $[1] ratio, $[1] and $[0] Ratio;

PHP code uses function encapsulation to facilitate analysis using

<?php
function insertSort(&$arr){
 for($i=1;$i<count($arr);$i++){
 //$insertVal是准备插入的数
 for($j=$i;$j>0;$j--){
  if($arr[$j]>$arr[$j-1]){
  $insertVal = $arr[$j];
  $arr[$j] = $arr[$j-1];
  $arr[$j-1] = $insertVal;
  }
 }
 }
}
$myarr = array(2,6,3,9);
insertSort($myarr);
echo "<pre class="brush:php;toolbar:false">";
print_r($myarr);
?>

code:

First big loop:

$i=1 Array (2,6,3,9)
$j=1 Perform the ratio of 6 and 2: $arr[1]=2;$arr[0]=6, get (6,2 ,3,9)

Second big loop:

$i=2 Array (6,2,3,9)
$j=2 Execute ratio of 3 and 2: Become $arr[2]=2, $arr[1]=3, get (6,3,2,9)
$j--,$j=1 Execute $arr[1] and $arr[ 0] Ratio: The condition is not established

The third big loop:

$i=3 Array (6,3,2,9)
$j=3 Execute 9 and 2 ratio : Become $arr[3]=2,$arr[2]=9, get (6,3,9,2)
$j--,$j=2 Execute the ratio of 9 and 3: become $ arr[2]=3,$arr[1]=9, get (6,9,3,2)
$j--,$j=1 Execute the ratio of 9 and 6: become $arr[1] =5,$arr[0]=9, get (9,6,3,2)

Summary: The above is the entire content of this article, I hope it can help everyone learn Helps.

Related recommendations:

How to send emails using php forms

How to add spaces to each paragraph in php

The above is the detailed content of Principle and application of insertion sorting method in php. 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