PHP's Heapsort

Source: Internet
Author: User
Tags array length explode sort
A program for practice heap sorting
?
Heap Sorting 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, and the $c represents the end point,
{
while ($b < $c)
{
$h 1=2* $b;
$h 2= (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 <count ($this->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;//Sort 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!! <br> ';
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 ' <font color=red> '. $ok [$i]. ' </font><br> ';
Else
echo$ok[$i]. ' <br> '; *
}
}
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 ' <font color=red> '. $ok [$i]. ' </font><br> ';
ElseIf ($ok [$j]> $ok [$i])
Echo ' <font color=green> '. $ok [$i]. ' </font><br> ';
Else
echo$ok[$i]. ' <br> '; *
}
}
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 ' <font color=red> '. $ok [$i]. ' </font><br> ';
ElseIf ($ok [$j]> $ok [$i])
Echo ' <font color=green> '. $ok [$i]. ' </font><br> ';
Else
echo$ok[$i]. ' <br> '; *
}
}
Else
{
Echo ' parameter input Error!! <br> ';
}
//////
$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 = ' regular sort ';
ElseIf ($check ==3)
$word = ' ordinary sort ';
Else
$word = ' no sort ';
Echo$word. ' A sort of array of '. $v. ' Elements, consuming '. ($ss $ss 1). ' Seconds time ';
//////
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.