首頁 > php教程 > php手册 > 排序算法

排序算法

WBOY
發布: 2016-06-20 08:42:12
原創
1002 人瀏覽過
  1. 冒泡排序

    • 实现原理

      ① 首先将所有待排序的数字放入工作列表中。

      ② 从列表的第一个数字到倒数第二个数字,逐个检查:若某一位上的数字大于他的下一位,则将它与它的下一位交换。

      ③ 重复步骤②,直至再也不能交换。

    • 代码实现

      复制代码
      <span style="color: #008080;"> 1</span> <span style="color: #000000;">php
      </span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">function</span> bubbingSort(<span style="color: #0000ff;">array</span> <span style="color: #800080;">$array</span><span style="color: #000000;">)
      </span><span style="color: #008080;"> 3</span> <span style="color: #000000;">{
      </span><span style="color: #008080;"> 4</span>     <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0, <span style="color: #800080;">$len</span>=<span style="color: #008080;">count</span>(<span style="color: #800080;">$array</span>)-1; <span style="color: #800080;">$i</span>$len; ++<span style="color: #800080;">$i</span><span style="color: #000000;">)
      </span><span style="color: #008080;"> 5</span> <span style="color: #000000;">    {
      </span><span style="color: #008080;"> 6</span>         <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$j</span>=<span style="color: #800080;">$len</span>; <span style="color: #800080;">$j</span>><span style="color: #800080;">$i</span>; --<span style="color: #800080;">$j</span><span style="color: #000000;">)
      </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">        {
      </span><span style="color: #008080;"> 8</span>             <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$array</span>[<span style="color: #800080;">$j</span>] $array[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">])
      </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">            {
      </span><span style="color: #008080;">10</span>                 <span style="color: #800080;">$temp</span> = <span style="color: #800080;">$array</span>[<span style="color: #800080;">$j</span><span style="color: #000000;">];
      </span><span style="color: #008080;">11</span>                 <span style="color: #800080;">$array</span>[<span style="color: #800080;">$j</span>] = <span style="color: #800080;">$array</span>[<span style="color: #800080;">$j</span>-1<span style="color: #000000;">];
      </span><span style="color: #008080;">12</span>                 <span style="color: #800080;">$array</span>[<span style="color: #800080;">$j</span>-1] = <span style="color: #800080;">$temp</span><span style="color: #000000;">;
      </span><span style="color: #008080;">13</span> <span style="color: #000000;">            }
      </span><span style="color: #008080;">14</span> <span style="color: #000000;">        }
      </span><span style="color: #008080;">15</span> <span style="color: #000000;">    }
      </span><span style="color: #008080;">16</span>     <span style="color: #0000ff;">return</span> <span style="color: #800080;">$array</span><span style="color: #000000;">;
      </span><span style="color: #008080;">17</span> <span style="color: #000000;">}
      </span><span style="color: #008080;">18</span> 
      <span style="color: #008080;">19</span> <span style="color: #0000ff;">print</span> '<pre class="brush:php;toolbar:false">'<span style="color: #000000;">;
      </span><span style="color: #008080;">20</span> <span style="color: #008080;">print_r</span>(bubbingSort(<span style="color: #0000ff;">array</span>(1,4,22,5,7,6,9<span style="color: #000000;">)));
      </span><span style="color: #008080;">21</span> <span style="color: #0000ff;">print</span> '
      登入後複製
      ';
      复制代码

       

  2. 快速排序

    • 实现原理

      采用分治的思想:先保证列表的前半部分都小于后半部分,然后分别对前半部分和后半部分排序,这样整个列表就有序了。

    • 代码实现

      复制代码
      <span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span> quickSort(<span style="color: #0000ff;">array</span> <span style="color: #800080;">$array</span><span style="color: #000000;">)
      </span><span style="color: #008080;"> 2</span> <span style="color: #000000;">{
      </span><span style="color: #008080;"> 3</span>     <span style="color: #800080;">$len</span> = <span style="color: #008080;">count</span>(<span style="color: #800080;">$array</span><span style="color: #000000;">);
      </span><span style="color: #008080;"> 4</span>     <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$len</span> )
      <span style="color: #008080;"> 5</span> <span style="color: #000000;">    {
      </span><span style="color: #008080;"> 6</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$array</span><span style="color: #000000;">;
      </span><span style="color: #008080;"> 7</span> <span style="color: #000000;">    }
      </span><span style="color: #008080;"> 8</span>     <span style="color: #800080;">$key</span> = <span style="color: #800080;">$array</span>[0<span style="color: #000000;">];
      </span><span style="color: #008080;"> 9</span>     <span style="color: #800080;">$left</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
      </span><span style="color: #008080;">10</span>     <span style="color: #800080;">$right</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
      </span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=1; <span style="color: #800080;">$i</span>$len; ++<span style="color: #800080;">$i</span><span style="color: #000000;">)
      </span><span style="color: #008080;">12</span> <span style="color: #000000;">    {
      </span><span style="color: #008080;">13</span>         <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$array</span>[<span style="color: #800080;">$i</span>] $key<span style="color: #000000;">)
      </span><span style="color: #008080;">14</span> <span style="color: #000000;">        {
      </span><span style="color: #008080;">15</span>             <span style="color: #800080;">$left</span>[] = <span style="color: #800080;">$array</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
      </span><span style="color: #008080;">16</span> <span style="color: #000000;">        }
      </span><span style="color: #008080;">17</span>         <span style="color: #0000ff;">else</span>
      <span style="color: #008080;">18</span> <span style="color: #000000;">        {
      </span><span style="color: #008080;">19</span>             <span style="color: #800080;">$right</span>[] = <span style="color: #800080;">$array</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">];
      </span><span style="color: #008080;">20</span> <span style="color: #000000;">        }
      </span><span style="color: #008080;">21</span> <span style="color: #000000;">    }
      </span><span style="color: #008080;">22</span>     <span style="color: #800080;">$left</span> = quickSort(<span style="color: #800080;">$left</span><span style="color: #000000;">);
      </span><span style="color: #008080;">23</span>     <span style="color: #800080;">$right</span> = quickSort(<span style="color: #800080;">$right</span><span style="color: #000000;">);
      </span><span style="color: #008080;">24</span>     <span style="color: #0000ff;">return</span> <span style="color: #008080;">array_merge</span>(<span style="color: #800080;">$left</span>, <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$key</span>), <span style="color: #800080;">$right</span><span style="color: #000000;">);
      </span><span style="color: #008080;">25</span> <span style="color: #000000;">}
      </span><span style="color: #008080;">26</span> 
      <span style="color: #008080;">27</span> <span style="color: #0000ff;">print</span> '<pre class="brush:php;toolbar:false">'<span style="color: #000000;">;
      </span><span style="color: #008080;">28</span> <span style="color: #008080;">print_r</span>(quickSort(<span style="color: #0000ff;">array</span>(1,4,22,5,7,6,9<span style="color: #000000;">)));
      </span><span style="color: #008080;">29</span> <span style="color: #0000ff;">print</span> '
      登入後複製
      ';
      复制代码

       

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