日期:2014-05-02  浏览次数:20454 次

练习堆排序的一个程序

<?
//堆排序应用
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];
             &nbs