php堆排序(heapsort)练习
2015-01-24信息快讯网
本文内容是和大家一起练习PHP堆排序的一个程序
<?
//堆排序应用
class heapsort
{
var $a;
function setarray($a)//取得数组
{
$this->a=$a;
}
function runvalue($b,$c)//$a 代表数组,$b代表排序堆,$c代表结束点,
{
while($b<$c)
{
$h1=2*$b;
$h2=(2*$b+1);
if($h1>$c)
break;
elseif($h1==$c)
{
if($this->a[$b]>$this->a[$h1])
{
$t=$this->a[$b];
$this->a[$b]=$this->a[$h1];
$this->a[$h1]=$t;
$la=1;
}
else
$la=1;
}
elseif(($this->a[$b]>$this->a[$h1])||($this->a[$b]>$this->a[$h2]))
{
if($this->a[$h1]>=$this->a[$h2])
{
$t=$this->a[$h2];
$this->a[$h2]=$this->a[$b];
$this->a[$b]=$t;
$b=$h2;
}
else
{
$t=$this->a[$h1];
$this->a[$h1]=$this->a[$b];
$this->a[$b]=$t;
$b=$h1;
}
}
else
$la=1;
if($la==1)
break;
}
}
function getarray()
{
$all=count($this->a);
$b=Floor(($all-1)/2);
for($i=$b;$i>=1;$i--)//先将数组建立成堆
{
$this->runvalue($i,($all-1));
}
for($i=1;$i<$all;$i++)
{
$a1=($all-$i);
if($i==1)
{
$t=$this->a[1];
$this->a[1]=$this->a[$a1];
$this->a[$a1]=$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)//取得数组
{
$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);
$st1=$st[0];
$st2=$st[1];
//////
$v=10000;//排序数组长度
$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'参数输入错误!!<br>';
}
//////
$s=microtime();
$st=explode(' ',$s);
$sta=$st[0];
$stb=$st[1];
$ss1=$sta-$st1;
$ss2=$stb-$st2;
if($check==1)
$word='堆排序';
elseif($check==2)
$word='常规排序';
elseif($check==3)
$word='普通排序';
else
$word='无排序';
echo$word.'对具有'.$v.'个元素的数组排序,消耗了'.($ss2+$ss1).'秒时间';
//////
?>
ThinkPHP CURD方法之order方法详解
ThinkPHP CURD方法之where方法详解
Yii结合CKEditor实现图片上传功能
PHP中多维数组的foreach遍历示例
Codeigniter出现错误提示Error with CACHE directory的解决方案
Codeigniter中禁止A Database Error Occurred错误提示的方法
CI框架中libraries,helpers,hooks文件夹详细说明
PHP、Nginx、Apache中禁止网页被iframe引用的方法
PHP 二维数组根据某个字段排序的具体实现
php数组中包含中文的排序方法
php伪静态之APACHE篇
PHP中数组的分组排序实例
php实例分享之二维数组排序
php冒泡排序、快速排序、快速查找、二维数组去重实例分享
php二维数组排序方法(array_multisort usort)
PHP反射类ReflectionClass和ReflectionObject的使用方法
php生成EAN_13标准条形码实例
PHP set_error_handler()函数使用详解(示例)
php使用exec shell命令注入的方法讲解
php使用mb_check_encoding检查字符串在指定的编码里是否有效
php foreach循环中使用引用的问题
php二维数组排序详解
Php header()函数语法及使用代码
php header功能的使用
php中OR与|| AND与&&的区别总结
PHP error_log()将错误信息写入一个文件(定义和用法)
使用PHP导出Word文档的原理和实例
php颜色转换函数hex-rgb(将十六进制格式转成十进制格式)
php 地区分类排序算法
PHP数组排序函数合集 以及它们之间的联系分析
PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解
PHP Class&Object -- PHP 自排序二叉树的深入解析
PHP array_multisort() 函数的深入解析
使用array mutisort 实现按某字段对数据排序
深入array multisort排序原理的详解
如何利用php array_multisort函数 对数据库结果进行复杂排序