首页 > 后端开发 > php教程 > php实现二路归并排序

php实现二路归并排序

WBOY
发布: 2016-07-29 08:51:31
原创
1028 人浏览过

<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> $m<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 $j<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> $m && <span>$j</span> $end<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> $m<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> $end<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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板