<?php
function quicksort( $array )
{
$cur = 1;
$stack[1]['l'] = 0;
$stack[1]['r'] = count($array)-1;
do
{
$l = $stack[$cur]['l'];
$r = $stack[$cur]['r'];
$cur--;
do
{
$i = $l;
$j = $r;
$tmp = $array[(int)( ($l+$r)/2 )][1];
// partion the array in two parts.
// left from $tmp are with smaller values,
// right from $tmp are with bigger ones
do
{
while( $array[$i][1] < $tmp )
$i++;
while( $tmp < $array[$j][1] )
$j--;
// swap elements from the two sides
if( $i <= $j )
{
$w = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $w;
$i++;
$j--;
}
}while( $i <= $j );
if( $i < $r )
{
$cur++;
$stack[$cur]['l'] = $i;
$stack[$cur]['r'] = $r;
}
$r = $j;
}while( $l < $r );
}while( $cur != 0 );
return $array;
}
function microtime_float()
{
return ((float)$usec + (float)$sec);
}
function set(&$uzytkownik)
{
$uzytkownik[0][0] = "Jasiu";
$uzytkownik[0][1] = 22;
$uzytkownik[1][0] = "Wacek";
$uzytkownik[1][1] = 21;
$uzytkownik[2][0] = "Misiek";
$uzytkownik[2][1] = 16;
$uzytkownik[3][0] = "Kasia";
$uzytkownik[3][1] = 11;
}
function cmpfunc($a, $b){return ($a[1] == $b[1]) ? 0 : ($a[1] < $b[1]) ? -1 : 1;}
$time = microtime_float();
for ($i = 0; $i<5000; $i++)
{
set($uzytkownik);
usort($uzytkownik, 'cmpfunc'); }
$time = microtime_float()-$time;
echo "usort: ".$time."<br>";
$time = microtime_float();
for ($i = 0; $i<5000; $i++)
{
set($uzytkownik);
$uzytkownik = quicksort($uzytkownik);
}
$time = microtime_float()-$time;
echo "quicksort: ".$time."<br>";
?>