php實作二路歸併排序

WBOY
發布: 2016-07-29 08:51:31
原創
1000 人瀏覽過

<span> 1</span><span>$arr</span> = [9, 43, 12, 0, 87, 1<span>];
</span><span> 2</span><span>function</span> merge_sort(&<span>$arr</span><span>){
</span><span> 3</span>     _merge_sort(<span>$arr</span>, <span>$arr</span>, 0, <span>count</span>(<span>$arr</span>) - 1<span>);
</span><span> 4</span><span>}
</span><span> 5</span><span> 6</span><span>function</span> _merge_sort(&<span>$s_arr</span>, &<span>$d_arr</span>, <span>$i</span>, <span>$j</span><span>){
</span><span> 7</span><span>if</span>(<span>$i</span> > <span>$j</span><span>){
</span><span> 8</span><span>return</span><span>;
</span><span> 9</span><span>    }
</span><span>10</span><span>if</span>(<span>$i</span> == <span>$j</span><span>){
</span><span>11</span><span>echo</span> 'aa'<span>;
</span><span>12</span><span>$d_arr</span>[<span>$i</span>] = <span>$s_arr</span>[<span>$i</span><span>];
</span><span>13</span><span>return</span><span>false</span><span>;
</span><span>14</span><span>    }
</span><span>15</span><span>$tmp_arr</span> = <span>array</span><span>();
</span><span>16</span><span>$m</span> = <span>intval</span>((<span>$i</span> + <span>$j</span>)/2<span>);
</span><span>17</span><span>echo</span><span>$m</span><span>;
</span><span>18</span><span>if</span>(<span>$i</span> <= <span>$m</span><span>){
</span><span>19</span>         _merge_sort(<span>$s_arr</span>, <span>$tmp_arr</span>, <span>$i</span>, <span>$m</span><span>);
</span><span>20</span><span>    }
</span><span>21</span><span>if</span>(<span>$m</span>+1 <= <span>$j</span><span> ){
</span><span>22</span>         _merge_sort(<span>$s_arr</span>, <span>$tmp_arr</span>, <span>$m</span>+1, <span>$j</span><span>);
</span><span>23</span><span>    }
</span><span>24</span>     merge(<span>$tmp_arr</span>, <span>$d_arr</span>, <span>$i</span>, <span>$m</span>, <span>$j</span><span>);
</span><span>25</span><span>}
</span><span>26</span> //$s_arr中的$start到$m与$m到$end两个序列都是有序的,将这两个序列合并到$d_arr里面
<span>27</span><span>function</span> merge(&<span>$s_arr</span>, &<span>$d_arr</span>, <span>$start</span>, <span>$m</span>, <span>$end</span><span>){
</span><span>28</span><span>$i</span> = <span>$start</span>; <span>$j</span> = <span>$m</span>+1;<span>$d_i</span> = <span>$i</span><span>;
</span><span>29</span><span>while</span>(<span>$i</span> <= <span>$m</span> && <span>$j</span> <= <span>$end</span><span>){
</span><span>30</span><span>if</span>(<span>$s_arr</span>[<span>$i</span>] > <span>$s_arr</span>[<span>$j</span><span>]){
</span><span>31</span><span>$d_arr</span>[<span>$d_i</span>++] = <span>$s_arr</span>[<span>$i</span>++<span>];
</span><span>32</span><span>//</span><span>$i++;</span><span>33</span>         }<span>else</span><span>{
</span><span>34</span><span>$d_arr</span>[<span>$d_i</span>++] = <span>$s_arr</span>[<span>$j</span>++<span>];
</span><span>35</span><span>        }
</span><span>36</span><span>    }
</span><span>37</span><span>while</span>(<span>$i</span> <= <span>$m</span><span>){
</span><span>38</span><span>$d_arr</span>[<span>$d_i</span>++] = <span>$s_arr</span>[<span>$i</span>++<span>];
</span><span>39</span><span>//</span><span>$i++;</span><span>40</span><span>    }
</span><span>41</span><span>while</span> (<span>$j</span> <= <span>$end</span><span>) {
</span><span>42</span><span>$d_arr</span>[<span>$d_i</span>++] = <span>$s_arr</span>[<span>$j</span>++<span>];
</span><span>43</span><span>    }
</span><span>44</span> }
登入後複製

以上就介紹了php實作二路歸併排序,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

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