Home > Backend Development > PHP Tutorial > Comprehensive implementation of PHP sorting algorithm_PHP tutorial

Comprehensive implementation of PHP sorting algorithm_PHP tutorial

WBOY
Release: 2016-07-15 13:27:29
Original
819 people have browsed it

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.

Today’s post just used PHP to implement the 4 sorting algorithm. In addition, heap sorting and merge sorting were not written. The time complexity of insertion sort, selection sort, and bubble sort seems to be O(N2), so it is of little practical significance. In the actual test, I performed it on 3,000 array elements. All three sorting algorithms cost 80 It takes about 2 seconds, while quick sort only takes 8 seconds. The gap is indeed quite large. If you are interested, you can test it yourself. Let’s take a detailed look at the implementation of your PHP sorting algorithm.
<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>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446512.htmlTechArticleWhen learning PHP, you may encounter PHP sorting problems. Here we will introduce the solutions to PHP sorting problems. Take it out here and share it with everyone. I always have to look at the data every three to five times every year...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template