Rumah > Artikel > pembangunan bahagian belakang > 全面实现PHP排序算法_PHP教程
学习PHP时,你可能会遇到 PHP排序问题,这里将介绍 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>