Thursday, September 17, 2009

insertion sort di php

$a = array(10,9,8,7,6,5,4,3,2,1,5,4,3,2,1);


insertionSort($a);

print_r($a);


function insertionSort(& $array)
{
$N = sizeof($array);

for($j=1;$j<$N;$j++)
{
$key = $array[$j];
$i = $j - 1;
while($i>=0 && $array[$i]>$key)
{
$array[$i+1] = $array[$i];
$i=$i-1;
}

$array[$i+1] = $key;
}
}

No comments:

Post a Comment