插入排序及优化

插入排序过程可以理解为排扑克牌 ,每次摸起一张 依次比较后插入到相应的位置,下面图片是从百度图库中找的。
插入排序及优化_第1张图片


 $arr = [53,27,36,15,69,42];


 function insert_sort($arr){
     $len = count($arr);
     
     //从第二个数开始是因为默认认为第一个数是排好序的
     for ($i=1; $i < $len; $i++) {
         //将第$i个数依次和前面排好的数比较插入到合适的位置
         for ($j=$i; $j > 0; $j--) { 
            //  print_r($j."\n");
             if($arr[$j] < $arr[$j-1]){
                 $temp = $arr[$j];
                 $arr[$j] = $arr[$j-1];
                 $arr[$j-1] = $temp;
             }
              print_r($arr);
         }
         print_r(str_repeat('-',20)."\n");
     }
     
     return $arr;
 }

运行过程

Array
(
    [0] => 27
    [1] => 53
    [2] => 36
    [3] => 15
    [4] => 69
    [5] => 42
)
--------------------
Array
(
    [0] => 27
    [1] => 36
    [2] => 53
    [3] => 15
    [4] => 69
    [5] => 42
)
Array
(
    [0] => 27
    [1] => 36
    [2] => 53
    [3] => 15
    [4] => 69
    [5] => 42
)
--------------------
Array
(
    [0] => 27
    [1] => 36
    [2] => 15
    [3] => 53
    [4] => 69
    [5] => 42
)
Array
(
    [0] => 27
    [1] => 15
    [2] => 36
    [3] => 53
    [4] => 69
    [5] => 42
)
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 53
    [4] => 69
    [5] => 42
)
--------------------
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 53
    [4] => 69
    [5] => 42
)
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 53
    [4] => 69
    [5] => 42
)
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 53
    [4] => 69
    [5] => 42
)
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 53
    [4] => 69
    [5] => 42
)
--------------------
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 53
    [4] => 42
    [5] => 69
)
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 42
    [4] => 53
    [5] => 69
)
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 42
    [4] => 53
    [5] => 69
)
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 42
    [4] => 53
    [5] => 69
)
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 42
    [4] => 53
    [5] => 69
)
--------------------

优化后的插入排序


 $arr = [53,27,36,15,69,42];


 function insert_sort($arr){
     $len = count($arr);
     
     //从第二个数开始是因为默认认为第一个数是排好序的
     for ($i=1; $i < $len; $i++) {
         //将第$i个数依次和前面排好的数比较插入到合适的位置

         //记住第i个数
         $temp = $arr[$i];
         for ($j=$i; $j > 0; $j--) { 
            //  print_r($j."\n");
            //找到$i要插入的位置
             if($temp < $arr[$j-1]){
                 $arr[$j] = $arr[$j-1];
             }else{
                 //中断循环保留$i的插入位置
                 break;
             }
             
         }
         //将保存的值插入到合适的位置去
         $arr[$j] = $temp;
         print_r($arr);
         print_r(str_repeat('-',20)."\n");
     }
     
     return $arr;
 }

运行过程

Array
(
    [0] => 27
    [1] => 53
    [2] => 36
    [3] => 15
    [4] => 69
    [5] => 42
)
--------------------
Array
(
    [0] => 27
    [1] => 36
    [2] => 53
    [3] => 15
    [4] => 69
    [5] => 42
)
--------------------
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 53
    [4] => 69
    [5] => 42
)
--------------------
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 53
    [4] => 69
    [5] => 42
)
--------------------
Array
(
    [0] => 15
    [1] => 27
    [2] => 36
    [3] => 42
    [4] => 53
    [5] => 69
)
--------------------

你可能感兴趣的:(通用技巧,php)