Every repetition of insertion sort removes an element from the input
data, inserting it into the correct position in the already-sorted list,
until no input elements remain. The choice of which element to remove
from the input is arbitrary, and can be made using almost any choice
algorithm.
<?php
$num=array(1,9,3,7,8,2,5,6);
function insertionsort($num)
{
for ($i=0;$i<=count($num)-1;$i++)
{
for($j=0;$j<$i;$j++)
{
if($num[$j]>$num[$i])
{
$temp=$num[$i];
$num[$i]=$num[$j];
$num[$j]=$temp;
}
}
//var_dump($num);
}
return $num;
}
var_dump($num);
var_dump(insertionsort($num));
?>
array
0 => int 1
1 => int 9
2 => int 3
3 => int 7
4 => int 8
5 => int 2
6 => int 5
7 => int 6
array
0 => int 1
1 => int 9
2 => int 3
3 => int 7
4 => int 8
5 => int 2
6 => int 5
7 => int 6
array
0 => int 1
1 => int 3
2 => int 9
3 => int 7
4 => int 8
5 => int 2
6 => int 5
7 => int 6
array
0 => int 1
1 => int 3
2 => int 7
3 => int 9
4 => int 8
5 => int 2
6 => int 5
7 => int 6
array
0 => int 1
1 => int 3
2 => int 7
3 => int 8
4 => int 9
5 => int 2
6 => int 5
7 => int 6
array
0 => int 1
1 => int 2
2 => int 3
3 => int 7
4 => int 8
5 => int 9
6 => int 5
7 => int 6
array
0 => int 1
1 => int 2
2 => int 3
3 => int 5
4 => int 7
5 => int 8
6 => int 9
7 => int 6
array
0 => int 1
1 => int 2
2 => int 3
3 => int 5
4 => int 6
5 => int 7
6 => int 8
7 => int 9