When learning PHP, you may encounter PHP sorting problems. Here we will introduce the solution to PHP sorting problems, and share them with you here. I always have to look at the data structure every three or five times every year, and every time I always feel that I haven't learned a lot of things well, alas.
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><?</SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>//插入排序(一维数组) </SPAN><LI class=alt><SPAN>function insert_sort($arr){ </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>count</SPAN><SPAN class=attribute-value>count</SPAN><SPAN> = count($arr); </SPAN></SPAN><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><SPAN>$count; $i++){ </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>tmp</SPAN><SPAN> = $arr[$i]; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>j</SPAN><SPAN> = $i - 1; </SPAN></SPAN><LI class=""><SPAN>while($arr[$j] </SPAN><SPAN class=tag>></span><span> $tmp){ </span></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><SPAN>$count; $i++){ </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>k</SPAN><SPAN> = $i; </SPAN></SPAN><LI class=alt><SPAN>for($</SPAN><SPAN class=attribute>j</SPAN><SPAN>=$i+1; $j</SPAN><SPAN class=tag><</SPAN><SPAN>$count; $j++){ </SPAN></SPAN><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><SPAN>= 0) return false; </SPAN></SPAN><LI class=""><SPAN> </SPAN><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><SPAN>$count; $i++){ </SPAN></SPAN><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><SPAN> $array[$j-1]){ </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>tmp</SPAN><SPAN> = $array[$j]; </SPAN></SPAN><LI class=alt><SPAN>$array[$j] = $array[$j-1]; </SPAN><LI class=""><SPAN>$array[$j-1] = $tmp; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>return $array; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>//快速排序(一维数组) </SPAN><LI class=""><SPAN>function quick_sort($array){ </SPAN><LI class=alt><SPAN>if (count($array) </SPAN><SPAN class=tag><</SPAN><SPAN>= 1) return $array; </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>key</SPAN><SPAN> = $array[0]; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>left_arr</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>array</SPAN><SPAN>(); </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>right_arr</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>array</SPAN><SPAN>(); </SPAN></SPAN><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><SPAN class=tag-name>count</SPAN><SPAN>($array); $i++){ </SPAN></SPAN><LI class=alt><SPAN>if ($array[$i] </SPAN><SPAN class=tag><</SPAN><SPAN>= $key) </SPAN></SPAN><LI class=""><SPAN>$left_arr[] = $array[$i]; </SPAN><LI class=alt><SPAN>else </SPAN><LI class=""><SPAN>$right_arr[] = $array[$i]; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute>left_arr</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>quick_sort</SPAN><SPAN>($left_arr); </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute>right_arr</SPAN><SPAN> = </SPAN><SPAN class=attribute-value>quick_sort</SPAN><SPAN>($right_arr); </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>return array_merge($left_arr, array($key), $right_arr); </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag>?></span><span> </span> </li> </ol>