heapsort(PHP)
Jun 21, 2016 am 09:16 AM練習(xí)堆排序的一個(gè)程序
//堆排序應(yīng)用
class heapsort
? {
??? var $a;
??? function setarray($a)//取得數(shù)組
????? {
??????? $this->a=$a;
????? }
??? function runvalue($b,$c)//$a 代表數(shù)組,$b代表排序堆,$c代表結(jié)束點(diǎn),
????? {
??????? while($b????????? {
??????????? $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--)//先將數(shù)組建立成堆
????????? {
??????????? $this->runvalue($i,($all-1));
????????? }
??????? for($i=1;$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)//取得數(shù)組
????? {
??????? $this->a=$a;
????? }
??? function runvalue($i)
????? {
??????? $max=$this->a[$i];
??????? $id=$i;
??????? for($j=($i+1);$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;$ia)-1);$i++)
????????? $this->runvalue($i);
??????? return $this->a;
????? }
? }
//////
$s=microtime();
$st=explode(' ',$s);
$st1=$st[0];
$st2=$st[1];
//////
$v=10000;//排序數(shù)組長(zhǎng)度
$brr[0]=0;
for($i=1;$i? {
??? $brr[$i]=rand();
? }
$check=2;//1 stand for heapsort 2 stand for another sort
echo'after sort!!
';
if($check==1)
? {
??? $arr=new heapsort;
??? $arr->setarray($brr);
??? $ok=$arr->getarray();
??? for($i=1;$i????? {
??????? $j=((($i+1)>($v-1))?($v-1):($i+1));
? /*
?if($ok[$j]????????? echo''.$ok[$i].'
';
??????? else
????????? echo$ok[$i].'
';*/
????? }
? }
elseif($check==2)
? {
??? $arr=new sortarr;
??? $arr->setarray($brr);
??? $ok=$arr->getarray();
??? for($i=1;$i????? {
??????? $j=((($i+1)>($v-1))?($v-1):($i+1));/*
??????? if($ok[$j]????????? echo''.$ok[$i].'
';
??????? elseif($ok[$j]>$ok[$i])
????????? echo''.$ok[$i].'
';
??????? else
????????? echo$ok[$i].'
';*/
????? }
? }
elseif($check==3)
? {
??? sort($brr);
??? $ok=$brr;
??? for($i=1;$i????? {
??????? $j=((($i+1)>($v-1))?($v-1):($i+1));/*
??????? if($ok[$j]????????? echo''.$ok[$i].'
';
??????? elseif($ok[$j]>$ok[$i])
????????? echo''.$ok[$i].'
';
??????? else
????????? echo$ok[$i].'
';*/
????? }
? }
else
? {
??? echo'參數(shù)輸入錯(cuò)誤!!
';
? }
//////
$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='常規(guī)排序';
elseif($check==3)
? $word='普通排序';
else
? $word='無(wú)排序';
echo$word.'對(duì)具有'.$v.'個(gè)元素的數(shù)組排序,消耗了'.($ss2+$ss1).'秒時(shí)間';
//////
?>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The top 10 Bitcoin trading platforms in 2025 are: Binance: Industry-leading trading volume, a wide range of crypto asset choices. OKX: Strong liquidity, low trading fees, advanced contract trading. Gate.io: Extensive crypto asset selection, low transaction fees, and support for international payments. Kraken: Highly secure, highly liquid, and provides fiat currency pairs. Gemini: Super secure, regulated in the United States, professional services for institutional investors. Bitstamp: an old trading platform, regulated, and highly liquid.

Recommended top ten cryptocurrency trading platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini, these exchanges have their own characteristics, and users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

Recommended safe and reliable bitcoin exchanges include Binance, OKX, Gate.io, Coinbase, Kraken and Huobi. 1. Binance supports more than 600 cryptocurrencies and provides multiple trading functions. 2. OKX is suitable for professional investors and provides a variety of trading methods. 3.Gate.io supports 1,400 cryptocurrencies and is highly secure. 4.Coinbase is suitable for beginners and has a user-friendly interface. 5.Kraken is known for its security and is suitable for institutional investors. 6.Huobi performed well in the Asian market.

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

Short-term speculation is suitable for traders with time and pressure resistance, while long-term investment is suitable for holders who believe in the long-term development of the industry. 1. Short-term is suitable for the early stage of a bull market and major events. The core strategies are trend tracking and news arbitrage, and strict stop loss is required. 2. It is suitable for the bottom and disruptive technology in the bear market in the long term. The coin selection criteria are excellent fundamentals and reasonable token economy, and fixed investment and dynamic stop-profit are required.

Top 10 Cryptocurrency Exchange Applications (2025): Binance: The maximum liquidity and asset options are wide. OKX: The first choice for cryptocurrency derivatives trading. Gate.io: specializes in spot trading and has a wide range of asset classes. Bitget: A choice for innovation in social trading and automated strategy.

Alipay and Bitcoin Transactions: A safe and convenient purchasing guide Alipay is a widely used payment tool in China. Although it does not support direct purchase of cryptocurrencies such as Bitcoin, you can complete transactions indirectly through some channels. This article will guide you on how to use Alipay to purchase Bitcoin safely and conveniently, and explain the risks that need to be paid attention to. The most common way to purchase Bitcoin indirectly through cryptocurrency exchanges is to trade through cryptocurrency exchanges that support Alipay. This method usually requires you to buy USDT (stablecoin) first and then exchange USDT for Bitcoin. The following is an example of Ouyi OKX Exchange, explain the operation steps in detail: Register Ouyi OKX account: Visit Ouyi OKX official website (click to register), register an account using email address, and complete

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:
