tirriainen
2/22/2018 - 6:08 AM

insertionsort.php

<?php 
function insertion($numbers) {
	for ($i=0; $i < count($numbers); $i++) {
		$value = $numbers[$i];
		$j = $i-1;
		while ($j >= 0 && $numbers[$j] > $value){
			$numbers[$j+1] = $numbers[$j];
			$j--;
		}
		$numbers[$j+1] = $value;
	}
	return $numbers;
}

$numbers = [];
while (true) {
	array_push($numbers, rand(0, 100));
	if (count($numbers) == 10) break;
}

echo implode(', ', insertion($numbers));