Home  >  Article  >  Backend Development  >  PHP insertion sort method to implement array sorting example, array example_PHP tutorial

PHP insertion sort method to implement array sorting example, array example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:26867browse

php insertion sort method to implement array sorting example, array example

The example in this article describes how to implement array sorting using PHP insertion sort. Share it with everyone for your reference. 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: Suppose (without actually creating) there is an ordered array $arr = array(2), use $arr[1]=6 to compare with it, if 6>2, use $arr[ 0] is then moved to position $arr[1], and 6 is inserted into position $arr[0]. 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 cycle: $[2] versus $[1], $[1] versus $[0];
The third big cycle: $[3] versus $[2], $[2] versus $[1], $[1] versus $[0];

PHP code uses function encapsulation for ease of use

<&#63;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);
&#63;>

Code implementation analysis:

The first big cycle:

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

Second cycle:

$i=2 array(6,2,3,9)
$j=2 performs the ratio of 3 and 2: it becomes $arr[2]=2, $arr[1]=3, and we get (6,3,2,9)
$j--,$j=1 Execute $arr[1] and $arr[0] ratio: the condition is not true

The third cycle:

$i=3 array(6,3,2,9)
$j=3 performs the ratio of 9 and 2: it becomes $arr[3]=2, $arr[2]=9, and we get (6,3,9,2)
$j--,$j=2 Execute the ratio of 9 and 3: it becomes $arr[2]=3, $arr[1]=9, and get (6,9,3,2)
$j--,$j=1 perform the ratio of 9 and 6: it becomes $arr[1]=5, $arr[0]=9, and we get (9,6,3,2)

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/959116.htmlTechArticlephp insertion sort method to implement array sorting example, array example This article describes the php insertion sort method to implement array sorting method . Share it with everyone for your reference. The specific analysis is as follows:...
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