> 백엔드 개발 > PHP 튜토리얼 > 全面实现PHP排序算法_PHP教程

全面实现PHP排序算法_PHP教程

WBOY
풀어 주다: 2016-07-15 13:27:29
원래의
819명이 탐색했습니다.

学习PHP时,你可能会遇到 PHP排序问题,这里将介绍 PHP排序问题的解决方法,在这里拿出来和大家分享一下。每年总是要隔三差五的看数据结构,每次总是觉得自己很多东西没有学好,唉。

今天贴刚使用php实现4的排序算法,另外堆排序和归并排序没有写。插入排序、选择排序、,冒泡排序,时间复杂度貌似都是 O(N2),所以实际意义不大,在实际测试中,我对3000个数组元素进行,这三种排序算法都需要花费80秒左右,而快速排序只需要8秒,差距确是比较大,有兴趣的可以自己测试一下。下面我们就详细的看看你PHP排序算法的实现吧。
<ol class="dp-xml">
<li class="alt"><span><span class="tag"></span><span> </span></span></li>
<li class=""><span>//插入排序(一维数组)  </span></li>
<li class="alt"><span>function insert_sort($arr){  </span></li>
<li class="">
<span>$</span><span class="attribute">count</span><span class="attribute-value">count</span><span> = count($arr);  </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>; $i</span><span class="tag"><span>$count; $i++){  </span></span>
</li>
<li class="">
<span>$</span><span class="attribute">tmp</span><span> = $arr[$i];  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">j</span><span> = $i - 1;  </span>
</li>
<li class="">
<span>while($arr[$j] </span><span class="tag">></span><span> $tmp){  </span>
</li>
<li class="alt"><span>$arr[$j+1] = $arr[$j];  </span></li>
<li class=""><span>$arr[$j] = $tmp;  </span></li>
<li class="alt"><span>$j--;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>return $arr;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span> </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>//选择排序(一维数组)  </span></li>
<li class="alt"><span>function select_sort($arr){  </span></li>
<li class="">
<span>$</span><span class="attribute">count</span><span class="attribute-value">count</span><span> = count($arr);  </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>; $i</span><span class="tag"><span>$count; $i++){  </span></span>
</li>
<li class="">
<span>$</span><span class="attribute">k</span><span> = $i;  </span>
</li>
<li class="alt">
<span>for($</span><span class="attribute">j</span><span>=$i+1; $j</span><span class="tag"><span>$count; $j++){  </span></span>
</li>
<li class="">
<span>if ($arr[$k] </span><span class="tag">></span><span> $arr[$j])  </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">k</span><span> = $j;  </span>
</li>
<li class=""><span>if ($k != $i){  </span></li>
<li class="alt">
<span>$</span><span class="attribute">tmp</span><span> = $arr[$i];  </span>
</li>
<li class=""><span>$arr[$i] = $arr[$k];  </span></li>
<li class="alt"><span>$arr[$k] = $tmp;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>return $arr;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>//冒泡排序(一维数组)   </span></li>
<li class="alt"><span>function bubble_sort($array){   </span></li>
<li class="">
<span>$</span><span class="attribute">count</span><span class="attribute-value">count</span><span> = count($array);   </span>
</li>
<li class="alt">
<span>if ($count </span><span class="tag"><span>= 0) return false;   </span></span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span>for($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">0</span><span>; $i</span><span class="tag"><span>$count; $i++){   </span></span>
</li>
<li class="">
<span>for($</span><span class="attribute">j</span><span>=$count-1; $j</span><span class="tag">></span><span>$i; $j--){   </span>
</li>
<li class="alt">
<span>if ($array[$j] </span><span class="tag"><span> $array[$j-1]){   </span></span>
</li>
<li class="">
<span>$</span><span class="attribute">tmp</span><span> = $array[$j];   </span>
</li>
<li class="alt"><span>$array[$j] = $array[$j-1];   </span></li>
<li class=""><span>$array[$j-1] = $tmp;   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span>}   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span>return $array;   </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>//快速排序(一维数组)   </span></li>
<li class=""><span>function quick_sort($array){   </span></li>
<li class="alt">
<span>if (count($array) </span><span class="tag"><span>= 1) return $array;   </span></span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute">key</span><span> = $array[0];   </span>
</li>
<li class="">
<span>$</span><span class="attribute">left_arr</span><span> = </span><span class="attribute-value">array</span><span>();   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">right_arr</span><span> = </span><span class="attribute-value">array</span><span>();   </span>
</li>
<li class="">
<span>for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>; $i</span><span class="tag"><span class="tag-name">count</span><span>($array); $i++){   </span></span>
</li>
<li class="alt">
<span>if ($array[$i] </span><span class="tag"><span>= $key)   </span></span>
</li>
<li class=""><span>$left_arr[] = $array[$i];   </span></li>
<li class="alt"><span>else   </span></li>
<li class=""><span>$right_arr[] = $array[$i];   </span></li>
<li class="alt"><span>}   </span></li>
<li class="">
<span>$</span><span class="attribute">left_arr</span><span> = </span><span class="attribute-value">quick_sort</span><span>($left_arr);   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">right_arr</span><span> = </span><span class="attribute-value">quick_sort</span><span>($right_arr);   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>return array_merge($left_arr, array($key), $right_arr);   </span></li>
<li class=""><span>}   </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span></span><span class="tag">?></span><span> </span>
</li>
</ol>
로그인 후 복사

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446512.htmlTechArticle学习PHP时,你可能会遇到 PHP排序问题,这里将介绍 PHP排序问题的解决方法,在这里拿出来和大家分享一下。每年总是要隔三差五的看数据...
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿