首页 > 开发 > PHP > 正文

php堆排序(heapsort)练习

2024-05-04 22:23:30
字体:
来源:转载
供稿:网友

代码如下:
<?
//堆排序应用
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;

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表