1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
function Tri()
{
$tab = array(5,8,9,5,0,4,10,4);
$n = 8;
$left = 1;
$pluspetitnontrie = 0;
while ($left < $n-1)
{
$right = $n-1;
while ($left <= $right)
{
$this->echoColored($tab,$n,$left,$pluspetitnontrie,$right);
echo (" => ");
if ( $tab[$left] == $tab[$pluspetitnontrie])
{
$left++;
}
else
{
if ( $tab[$left] > $tab[$pluspetitnontrie])
{
$tmp = $tab[$right];
$tab[$right] = $tab[$left];
$tab[$left]=$tmp;
$right --;
}
else
{
$tmp = $tab[$pluspetitnontrie];
$tab[$pluspetitnontrie] = $tab[$left];
$tab[$left] = $tmp;
$left = $pluspetitnontrie+1;
}
}
$this->echoColored($tab,$n,$left,$pluspetitnontrie,$right);
echo('<BR>');
}
echo ('<br>Element '.$tab[$pluspetitnontrie].' Trie<br><br>');
$pluspetitnontrie = $left;
}
}
function echoColored($tab,$n,$left,$petit,$right)
{
echo ('{ ');
for ($i = 0; $i<$n;$i++)
{
if ($i>0) echo(',');
if ($i == $petit) echo('[ B][ U]');
if ($i == $left) echo('[ color="lime"]');
if ($i == $right) echo('[ color="red"]');
echo ( $tab[$i]);
if ($i == $right) echo('[ /color]');
if ($i == $left) echo('[ /color]');
if ($i == $petit) echo('[ /U][ /B] ');
}
echo ('}');
} |