• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php堆排序(heapsort)练习_php实例

php 搞代码 3年前 (2022-01-25) 12次浏览 已收录 0个评论
<?<BR>//堆排序应用<BR>class heapsort<BR>  {<BR>    var $a;<BR>    function setarray($a)//取得数组<BR>      {<BR>        $this->a=$a;<BR>      }<BR>    function runvalue($b,$c)//$a 代表数组,$b代表排序堆,$c代表结束点,<BR>      {<BR>        while($b<$c)<BR>          {<BR>            $h1=2*$b;<BR>            $h2=(2*$b+1);<BR>            if($h1>$c)<BR>              break;<BR>            elseif($h1==$c)<BR>              {<BR>                if($this->a[$b]>$this->a[$h1])<BR>                  {<BR>                    $t=$this->a[$b];<BR>                    $this->a[$b]=$this->a[$h1];<BR>                    $this->a[$h1]=$t;<BR>                    $la=1;<BR>                  }<BR>                else<BR>                  $la=1;<BR>              }<BR>            elseif(($this->a[$b]>$this->a[$h1])||($this->a[$b]>$this->a[$h2]))<BR>              {<BR>                if($this->a[$h1]>=$this->a[$h2])<BR>                  {<BR>                    $t=$this->a[$h2];<BR>                    $this->a[$h2]=$this->a[$b];<BR>                    $this->a[$b]=$t;<BR>                    $b=$h2;<BR>                  }<BR>                else<BR>                  {<BR>                    $t=$this->a[$h1];<BR>                    $this->a[$h1]=$this->a[$b];<BR>                    $this->a[$b]=$t;<BR>                    $b=$h1;<BR>                  }<BR>              }<BR>            else<BR>              $la=1;<BR>            if($la==1)<BR>              break;<BR>          }<BR>      }<BR>    function getarray()<BR>      {<BR>        $all=count($this->a);<BR>        $b=Floor(($all-1)/2);<BR>        for($i=$b;$i>=1;$i--)//先将数组建立成堆<BR>          {<BR>            $this->runvalue($i,($all-1));<BR>          }<BR>        for($i=1;$i<$all;$i++)<BR>          {<BR>            $a1=($all-$i);<BR>            if($i==1)<BR>              {<BR>                $t=$this->a[1];<BR>                $this->a[1]=$this->a[$a1];<BR>                $this->a[$a1]=$t;<BR>              }<BR>            else<BR>              {<BR>                $end=($all-$i);<BR>                $this->runvalue(1,$end);<BR>                $t=$this->a[1];<BR>                $this->a[1]=$this->a[$end];<BR>                $this->a[$end]=$t;<BR>              }<BR>          }<BR>        return $this->a;<BR>      }<BR>  }<BR>//////<BR>class sortarr<BR>  {<BR>    var $a;<BR>    function setarray($a)//取得数组<BR>      {<BR> <strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码*网/</strong><strong>搞gaodaima代码</strong>       $this->a=$a;<BR>      }<BR>    function runvalue($i)<BR>      {<BR>        $max=$this->a[$i];<BR>        $id=$i;<BR>        for($j=($i+1);$ja);$j++)<BR>          {<BR>            if($this->a[$j]>$max)<BR>              {<BR>                $max=$this->a[$j];<BR>                $id=$j;<BR>              }<BR>          }<BR>        if($id!=$i)<BR>          {<BR>            $t=$this->a[$id];<BR>            $this->a[$id]=$this->a[$i];<BR>            $this->a[$i]=$t;<BR>          }<BR>      }<BR>    function getarray()<BR>      {<BR>        for($i=1;$ia)-1);$i++)<BR>          $this->runvalue($i);<BR>        return $this->a;<BR>      }<BR>  }<BR>//////<BR>$s=microtime();<BR>$st=explode(' ',$s);<BR>$st1=$st[0];<BR>$st2=$st[1];<BR>//////<BR>$v=10000;//排序数组长度<BR>$brr[0]=0;<BR>for($i=1;$i<$v;$i++)<BR>  {<BR>    $brr[$i]=rand();<BR>  }<BR>$check=2;//1 stand for heapsort 2 stand for another sort<BR>echo'after sort!!<br>';<BR>if($check==1)<BR>  {<BR>    $arr=new heapsort;<BR>    $arr->setarray($brr);<BR>    $ok=$arr->getarray();<BR>    for($i=1;$i<$v;$i++)<BR>      {<BR>        $j=((($i+1)>($v-1))?($v-1):($i+1));<BR>  /*<BR> if($ok[$j]<$ok[$i])<BR>          echo'<font color="red">'.$ok[$i].'</font><br>';<BR>        else<BR>          echo$ok[$i].'<br>';*/<BR>      }<BR>  }<BR>elseif($check==2)<BR>  {<BR>    $arr=new sortarr;<BR>    $arr->setarray($brr);<BR>    $ok=$arr->getarray();<BR>    for($i=1;$i<$v;$i++)<BR>      {<BR>        $j=((($i+1)>($v-1))?($v-1):($i+1));/*<BR>        if($ok[$j]<$ok[$i])<BR>          echo'<font color="red">'.$ok[$i].'</font><br>';<BR>        elseif($ok[$j]>$ok[$i])<BR>          echo'<font color="green">'.$ok[$i].'</font><br>';<BR>        else<BR>          echo$ok[$i].'<br>';*/<BR>      }<BR>  }<BR>elseif($check==3)<BR>  {<BR>    sort($brr);<BR>    $ok=$brr;<BR>    for($i=1;$i<$v;$i++)<BR>      {<BR>        $j=((($i+1)>($v-1))?($v-1):($i+1));/*<BR>        if($ok[$j]<$ok[$i])<BR>          echo'<font color="red">'.$ok[$i].'</font><br>';<BR>        elseif($ok[$j]>$ok[$i])<BR>          echo'<font color="green">'.$ok[$i].'</font><br>';<BR>        else<BR>          echo$ok[$i].'<br>';*/<BR>      }<BR>  }<BR>else<BR>  {<BR>    echo'参数输入错误!!<br>';<BR>  }<BR>//////<BR>$s=microtime();<BR>$st=explode(' ',$s);<BR>$sta=$st[0];<BR>$stb=$st[1];<BR>$ss1=$sta-$st1;<BR>$ss2=$stb-$st2;<BR>if($check==1)<BR>  $word='堆排序';<BR>elseif($check==2)<BR>  $word='常规排序';<BR>elseif($check==3)<BR>  $word='普通排序';<BR>else<BR>  $word='无排序';<BR>echo$word.'对具有'.$v.'个元素的数组排序,消耗了'.($ss2+$ss1).'秒时间';<BR>//////<BR>?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php堆排序(heapsort)练习_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址