<?php insertionsort(array(1, 4, 3, 5, 6, 2)); function insertionSort(array $array) { $length = count($array); for ($i = 1; $i < $length; $i++) { $element = $array[$i]; $j = $i; while ($j > 0 && $array[$j - 1] > $element) { //move value to right and key to previous smaller index $array[$j] = $array[$j - 1]; $j = $j - 1; } //put the element at index $j $array[$j] = $element; echo implode(' ', $array) . "\n"; } return $array; } /* $t = fgets(STDIN); $string = fgets(STDIN); insertionsort(explode(' ', $string)); function insertionsort(array $input) { $length = count($input); for ($i = $length - 1; $i >= 0; $i-- ) { $t = $input[$i];
<?php //insertionsort(array(1,3,5,9,13,22,27,35,46,51,55,83,87,23)); insertionsort(array(2, 3, 4, 5, 6, 7, 8, 10, 9, 11)); function insertionsort(array $input) { $t = $input[count($input) - 1]; for ($i = count($input) - 1; $i >= 0; $i--) { if ($input[$i - 1] > $t) { $input[$i] = $input[$i - 1]; echo implode(' ', $input) . "\n"; } else { $input[$i] = $t; echo implode(' ', $input) . "\n"; break; } } } /* $t = fgets(STDIN); $string = fgets(STDIN); insertionsort(explode(' ', $string)); function insertionsort(array $input) { $t = $input[count($input) - 1]; for ($i = count($input) - 1; $i > 0; $i-- ) { if ($input[$i-1] > $t) { $input[$i] = $input[$i-1]; } else {