①まず整理したい数字をすべて作業リストに入れます。
② リストの最初の数字から最後から2番目の数字までを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> '
分割統治の考え方を採用します。まずリストの前半が後半よりも小さいことを確認してから、リスト全体が収まるように前半と後半をそれぞれ並べ替えます。注文。
<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> '