首页 > 学院 > 开发设计 > 正文

Insertion Sort

2019-11-15 01:54:29
字体:
来源:转载
供稿:网友
Insertion Sort

insertion sort wikipedia

 1 <?php 2 function swap(&$a, &$b){ 3     $c = $a; 4     $a = $b; 5     $b = $c; 6 } 7  8 # insertion sort 9 # ascend10 function sortInsertion(&$a){ # a is an array of numbers11 12     # length of a13     $m = count($a);14 15     if($m < 2){16         return;17     }18 19     # for m numbers, we have m-1 numbers to insert20     for($i=1; $i<=$m-1; $i++){21         for($j=$i; $j>0; $j--){22             if($a[$j] < $a[$j-1]){23                 swap($a[$j], $a[$j-1]);24             }25         }26     }27 28     return;29 }30 31 $arr = range(5, 0);32 sortInsertion($arr);33 echo implode(', ', $arr);34 35 // 0, 1, 2, 3, 4, 536 ?>


上一篇:Selection Sort

下一篇:Quick Sort

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表