1. 程式人生 > >PHP 演算法 插入排序

PHP 演算法 插入排序

function InsertSort(array $container)
{
$count = count($container);
for ($i = 1; $i < $count; $i++){
$temp = $container[$i];
$j = $i - 1;
while($j >= 0 && $container[$j] > $temp){
$container[$j+1] = $container[$j];
$j--;
}
if($i != $j+1)
$container[$j+1] = $temp;
}
return $container;
}

引用自 https://github.com/PuShaoWei/arithmetic-php,演算法時間複雜度為O(n^2),只適用於小部分資料排序