$arr = [1, 8, 7, 5, 4, 2, 11, 9, 20];
Bubble Sort:
for($i= 0;$i<Count($arr);$i++) { for($j= 0;$j<Count($arr) -$i-1;$j++) { if($arr[$j] >$arr[$j+ 1]) { $item=$arr[$j]; $arr[$j] =$arr[$j+ 1]; $arr[$j+ 1] =$item; } }}
Insert Sort:
for($c= 1;$c=Count($arr);$c++) { $value=$arr[$c]; $i=$c-1; while($i> = 0 and$arr[$i] >$value) { //$arr [i + 1] for the underlying value is replaced with $arr[$i] as the underlying value; $arr[$i+ 1] =$arr[$i]; $i=$i-1; $arr[$i+ 1] =$value; }}
Select Sort:
for($c= 0;$c<Count($arr);$c++) { //set the initial position $p $p=$c; for($j=$c+ 1;$j<Count($arr);$j++) {//when $c is 1 o'clock, $p is 1, $j is 2 ... if($arr[$p] >$arr[$j]) {//when labeled $p > Subscript as $j, $p = $j; $p=$j; } } if($p!=$c) {//at this point the $p is 2, $c is 1, then replace the value where they are located ... $item=$arr[$p]; $arr[$p] =$arr[$c]; $arr[$c] =$item; }}
PHP bubble Sort, select sort, insert sort