Skip to content

Commit

Permalink
Merge pull request wangzheng0822#294 from git-zjx/patch-5
Browse files Browse the repository at this point in the history
PHP Notice:  Undefined offset
  • Loading branch information
wangzheng0822 authored Apr 11, 2019
2 parents 5d849d4 + 1bb8c3d commit 02cfee4
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions php/11_sort/Sort.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<?php

function insertSort(&$arr)
function insertionSort(&$arr)
{
$i = 0;
$len = count($arr);
$n = count($arr);
if ($n <= 1) return;

while($i < $len){
$data = $arr[$i+1];
for ($j = $i;$j >=0 ;$j-- ){
if ($data >= $arr[$j]){
array_splice($arr, $i+1, 1);
array_splice($arr, ++$j, 0, $data);
for ($i = 1; $i < $n; ++$i) {
$value = $arr[$i];
$j = $i - 1;
// 查找插入的位置
for (; $j >= 0; --$j) {
if ($arr[$j] > $value) {
$arr[$j + 1] = $arr[$j]; // 数据移动
} else {
break;
}
}

$i++;
$arr[$j + 1] = $value; // 插入数据
}
}

$arr = [1,4,6,2,3,5,4];
insertSort($arr);
var_dump($arr);
insertionSort($arr);
var_dump($arr);

0 comments on commit 02cfee4

Please sign in to comment.