Copy CodeThe code is as follows:
Heap Sort Application
Class Heapsort
{
var $a;
function SetArray ($a)//Get array
{
$this->a= $a;
}
function Runvalue ($b, $c)//$a represents an array, $b represents the sort heap, $c represents the end point,
{
while ($b < $c)
{
$h 1=2* $b;
$h 2= ($b + 1);
if ($h 1> $c)
Break
ElseIf ($h 1== $c)
{
if ($this->a[$b]> $this->a[$h 1])
{
$t = $this->a[$b];
$this->a[$b]= $this->a[$h 1];
$this->a[$h 1]= $t;
$la = 1;
}
Else
$la = 1;
}
ElseIf (($this->a[$b]> $this->a[$h 1]) | | ($this->a[$b]> $this->a[$h 2]))
{
if ($this->a[$h 1]>= $this->a[$h 2])
{
$t = $this->a[$h 2];
$this->a[$h 2]= $this->a[$b];
$this->a[$b]= $t;
$b = $h 2;
}
Else
{
$t = $this->a[$h 1];
$this->a[$h 1]= $this->a[$b];
$this->a[$b]= $t;
$b = $h 1;
}
}
Else
$la = 1;
if ($la ==1)
Break
}
}
function GetArray ()
{
$all =count ($this->a);
$b =floor (($all-1)/2);
for ($i = $b; $i >=1; $i-)/Set up the array first
{
$this->runvalue ($i, ($all-1));
}
for ($i =1; $i < $all; $i + +)
{
$a 1= ($all-$i);
if ($i ==1)
{
$t = $this->a[1];
$this->a[1]= $this->a[$a 1];
$this->a[$a 1]= $t;
}
Else
{
$end = ($all-$i);
$this->runvalue (1, $end);
$t = $this->a[1];
$this->a[1]= $this->a[$end];
$this->a[$end]= $t;
}
}
return $this->a;
}
}
//////
Class Sortarr
{
var $a;
function SetArray ($a)//Get array
{
$this->a= $a;
}
function Runvalue ($i)
{
$max = $this->a[$i];
$id = $i;
for ($j = ($i + 1); $j a); $j + +)
{
if ($this->a[$j]> $max)
{
$max = $this->a[$j];
$id = $j;
}
}
if ($id! = $i)
{
$t = $this->a[$id];
$this->a[$id]= $this->a[$i];
$this->a[$i]= $t;
}
}
function GetArray ()
{
for ($i =1; $i < (COUNT ($this->a)-1); $i + +)
$this->runvalue ($i);
return $this->a;
}
}
//////
$s =microtime ();
$st =explode (' ', $s);
$st 1= $st [0];
$st 2= $st [1];
//////
$v =10000;//sorted Array length
$BRR [0]=0;
for ($i =1; $i < $v; $i + +)
{
$BRR [$i]=rand ();
}
$check =2;//1 stand for Heapsort 2 stand for another sort
Echo ' After sort!!
';
if ($check ==1)
{
$arr =new Heapsort;
$arr->setarray ($BRR);
$ok = $arr->getarray ();
for ($i =1; $i < $v; $i + +)
{
$j = ((($i + 1) > ($v-1))? ( $V-1):($i + 1));
/*
if ($ok [$j]< $ok [$i])
Echo '. $ok [$i]. '
';
Else
echo$ok[$i]. '
';*/
}
}
ElseIf ($check ==2)
{
$arr =new Sortarr;
$arr->setarray ($BRR);
$ok = $arr->getarray ();
for ($i =1; $i < $v; $i + +)
{
$j = ((($i + 1) > ($v-1))? ( $V-1):($i + 1));/*
if ($ok [$j]< $ok [$i])
Echo '. $ok [$i]. '
';
ElseIf ($ok [$j]> $ok [$i])
Echo '. $ok [$i]. '
';
Else
echo$ok[$i]. '
';*/
}
}
ElseIf ($check ==3)
{
Sort ($BRR);
$ok = $BRR;
for ($i =1; $i < $v; $i + +)
{
$j = ((($i + 1) > ($v-1))? ( $V-1):($i + 1));/*
if ($ok [$j]< $ok [$i])
Echo '. $ok [$i]. '
';
ElseIf ($ok [$j]> $ok [$i])
Echo '. $ok [$i]. '
';
Else
echo$ok[$i]. '
';*/
}
}
Else
{
Echo ' parameter input Error!!
';
}
//////
$s =microtime ();
$st =explode (' ', $s);
$sta = $st [0];
$STB = $st [1];
$ss 1= $sta-$st 1;
$ss 2= $stb-$st 2;
if ($check ==1)
$word = ' heap sort ';
ElseIf ($check ==2)
$word = ' General sort ';
ElseIf ($check ==3)
$word = ' normal sort ';
Else
$word = ' no sort ';
Echo$word. ' To have '. $v. ' An array of elements is consumed '. ($ss $ss 1). ' Seconds time ';
//////
?>
http://www.bkjia.com/PHPjc/825187.html www.bkjia.com true http://www.bkjia.com/PHPjc/825187.html techarticle Copy the code as follows:?//Heap Sort Application class Heapsort {var $a, function SetArray ($a)//get array {$this-a= $a;} function Runvalue ($b, $c)//$a Represents an array, $b represents a sort ...