<?<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>