PHP排序問題

來源:互聯網
上載者:User

簡介:這是PHP排序問題的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=339370' scrolling='no'>

  最後一科TCP/IP考試的結束,我的大二生活就這樣被無情結束了。早上起來,看看哥們們還在酣睡,不知道自己該幹點什麼,遂開始流浪。看到某兄寫的用冒泡排序演算法給PHP中的數組進行排序,心中不免有些暗自發笑——PHP中那麼多排序函數還自己寫這個幹嗎?!後來想想,原來我錯了,編程並不是死記硬背的機械運動,更是多種思維的集合;經常練習演算法的目的就是擴充自己思維的目的。

  遂自己也試著寫了寫……

<?php
$array = array(10,5,36,86,2,56,9,15,8,4,3,41);
$num = count($array);
for($i = 0;$i<$num-1;$i++){
for($j = $i+1;$j<$num;$j++){
if($array[$j]>$array[$i]){//這裡交換數組的值
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] =$temp;
}
}
}
print_r($array);
?>

  這是寫的冒泡排序演算法,結果還是可以的:

---------- PHP Debug ----------
Array
(
[0] => 86
[1] => 56
[2] => 41
[3] => 36
[4] => 15
[5] => 10
[6] => 9
[7] => 8
[8] => 5
[9] => 4
[10] => 3
[11] => 2
)

輸出完成 (耗時 0 秒) - 正常終止

  看著有了成功的小喜悅,就又寫了一個,於是問題就出來了——

<?php
$array = array(10,5,36,86,2,56,9,15,8,4,3,41);
$num = count($array);
for($i=0;$i<$num-1;$i++){
for($j=$i+1;$j<$num;$j++){
if($array[$j]>$array[$i]){//這裡是交換索引值
$temp = $i;
$i=$j;
$j=$temp;
}
}
}
print_r($array);
?>

  結果慘不忍睹啊……

---------- PHP Debug ----------
Array
(
[0] => 10
[1] => 5
[2] => 36
[3] => 86
[4] => 2
[5] => 56
[6] => 9
[7] => 15
[8] => 8
[9] => 4
[10] => 3
[11] => 41
)

輸出完成 (耗時 0 秒) - 正常終止

  一個也沒換過來,Why??Why???

  路過個別忘提個意見啊……

愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具

http://biancheng.dnbcw.info/php/339370.html pageNo:8

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.