Maison > php教程 > php手册 > 排序算法

排序算法

WBOY
Libérer: 2016-06-20 08:42:12
original
995 Les gens l'ont consulté
  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> '
      Copier après la connexion
      ';
      复制代码

       

  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> '
      Copier après la connexion
      ';
      复制代码

       

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal