首頁 > 後端開發 > php教程 > php 冒泡排序 快速排序

php 冒泡排序 快速排序

WBOY
發布: 2016-07-29 09:10:01
原創
816 人瀏覽過

/******
1)冒泡排序:兩兩交換數值,最小的值在最左邊,就如最輕的氣泡在最上邊。
2)對整數列數兩兩交換一次,最小的數在最左邊,每次都能得一個在剩下的數中的最小的數,「冒」出來的數組成一個有序區間,剩下的數值組成一無序區間,且有序區間中每一元素值都比無序區間的小。
3)快速排序:基準數,左右二個數組,遞歸調用,合併。
4)插入排序:排序區間分成二部分,左邊有序,右邊無序,從右區間取第一個元素插入左區間,若此元素比左邊區間最右邊的元素大,留在原處,若此元素比左邊區間最右邊的元素小,則插在最右邊5)元素的原位置,同時最右邊元素右移一位,計算器減一,重新和前面的元素比較,直到前面的元素比若要插入元素小為止,重複上述步驟。
6)注意區間端點值的處理,及數組的第一個元素下標為0.
***/

<span><br>$a</span>=<span>array</span>('3','8','1','4','11','7'<span>);
</span><span>print_r</span>(<span>$a</span><span>);
</span><span>$len</span> = <span>count</span>(<span>$a</span><span>);
</span><span>//</span><span>从小到大</span><span>for</span>(<span>$i</span>=1;<span>$i</span><<span>$len</span>;<span>$i</span>++<span>)
{
</span><span>for</span>(<span>$j</span>=<span>$len</span>-1;<span>$j</span>>=<span>$i</span>;<span>$j</span>--<span>)
</span><span>if</span>(<span>$a</span>[<span>$j</span>]<<span>$a</span>[<span>$j</span>-1<span>])
{</span><span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了<span>$x</span>=<span>$a</span>[<span>$j</span><span>];
 </span><span>$a</span>[<span>$j</span>]=<span>$a</span>[<span>$j</span>-1<span>];
 </span><span>$a</span>[<span>$j</span>-1]=<span>$x</span><span>;
}
}
</span><span>print_r</span>(<span>$a</span><span>);
</span><span>//</span><span>另一种方法 从小到大</span><span>$b</span>=<span>array</span>('4','3','8','9','2','1'<span>);
</span><span>$len</span>=<span>count</span>(<span>$b</span><span>);
</span><span>for</span>(<span>$k</span>=1;<span>$k</span><<span>$len</span>;<span>$k</span>++<span>)
{
</span><span>for</span>(<span>$j</span>=<span>$len</span>-1,<span>$i</span>=0;<span>$i</span><<span>$len</span>-<span>$k</span>;<span>$i</span>++,<span>$j</span>--<span>)
</span><span>if</span>(<span>$b</span>[<span>$j</span>]<<span>$b</span>[<span>$j</span>-1<span>]){
</span><span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了<span>$tmp</span>=<span>$b</span>[<span>$j</span><span>];
 </span><span>$b</span>[<span>$j</span>]=<span>$b</span>[<span>$j</span>-1<span>];
 </span><span>$b</span>[<span>$j</span>-1]=<span>$tmp</span><span>;
}
</span><span>print_r</span>(<span>$b</span><span>);
</span><span>echo</span> "
"<span>;
}
</span><span>//</span><span>下面的这个执行效率更高</span><span>function</span> maopao(<span>$arr</span><span>)
{
 </span><span>$len</span> = <span>count</span>(<span>$arr</span><span>);
 </span><span>for</span>(<span>$i</span>=1; <span>$i</span><<span>$len</span>; <span>$i</span>++)<span>//</span><span>最多做n-1趟排序</span><span> {
  </span><span>$flag</span> = <span>false</span>;    <span>//</span><span>本趟排序开始前,交换标志应为假</span><span>for</span>(<span>$j</span>=<span>$len</span>-1;<span>$j</span>>=<span>$i</span>;<span>$j</span>--<span>)
  {
   </span><span>if</span>(<span>$arr</span>[<span>$j</span>]<<span>$arr</span>[<span>$j</span>-1])<span>//</span><span>交换记录</span>   {<span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($arr[$j]>$arr[$j-1])就可以了<span>$x</span>=<span>$arr</span>[<span>$j</span><span>];
     </span><span>$arr</span>[<span>$j</span>]=<span>$arr</span>[<span>$j</span>-1<span>];
     </span><span>$arr</span>[<span>$j</span>-1]=<span>$x</span><span>;
     </span><span>$flag</span> = <span>true</span>;<span>//</span><span>发生了交换,故将交换标志置为真</span><span>   }
  }
  </span><span>if</span>(! <span>$flag</span>)<span>//</span><span>本趟排序未发生交换,提前终止算法</span><span>return</span><span>$arr</span><span>;   
 }
}
</span><span>$shuz</span> = <span>array</span>('2','4','1','8','5'<span>);
</span><span>$bb</span> = maopao(<span>$shuz</span><span>);
</span><span>print_r</span>(<span>$bb</span><span>);
</span><span>//</span><span> 快速排序</span><span>function</span> kuaisu(<span>$arr</span><span>){
    </span><span>$len</span> = <span>count</span>(<span>$arr</span><span>);
    </span><span>if</span>(<span>$len</span> <= 1<span>){
        </span><span>return</span><span>$arr</span><span>;
    }
    </span><span>$key</span> = <span>$arr</span>[0<span>];
    </span><span>$left_arr</span> = <span>array</span><span>();
    </span><span>$right_arr</span> = <span>array</span><span>();
    </span><span>for</span>(<span>$i</span>=1; <span>$i</span><<span>$len</span>;<span>$i</span>++<span>){
        </span><span>if</span>(<span>$arr</span>[<span>$i</span>] <= <span>$key</span><span>){
            </span><span>$left_arr</span>[] = <span>$arr</span>[<span>$i</span><span>];
        }</span><span>else</span><span>{
            </span><span>$right_arr</span>[] = <span>$arr</span>[<span>$i</span><span>];
        }
    }
    </span><span>$left_arr</span> = kuaisu(<span>$left_arr</span><span>);
    </span><span>$right_arr</span> = kuaisu(<span>$right_arr</span><span>);
    </span><span>return</span><span>array_merge</span>(<span>$left_arr</span>, <span>array</span>(<span>$key</span>), <span>$right_arr</span><span>);
}
</span><span>$arr</span> = <span>array</span>(23,98,54,2,9,62,34<span>);
</span><span>print_r</span>(kuaisu(<span>$arr</span>));
登入後複製

以上就介紹了php 冒泡排序 快速排序,包括了插入排序方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板