Home > php教程 > php手册 > body text

PHP 四种基本排序算法的代码实现(1)

WBOY
Release: 2016-06-13 09:11:05
Original
866 people have browsed it

PHP 四种基本排序算法的代码实现(1)

许多人都说算法是程序的核心,算法的好坏决定了程序的质量。作为一个初级phper,虽然很少接触到算法方面的东西。但是对于基本的排序算法还是应该掌握的,它是程序开发的必备工具。这里介绍冒泡排序,插入排序,选择排序,快速排序四种基本算法,分析一下算法的思路。

前提:分别用冒泡排序法,快速排序法,选择排序法,插入排序法将下面数组中的值按照从小到大的顺序进行排序。

$arr(1,43,54,62,21,66,32,78,36,76,39);

1. 冒泡排序

思路分析:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即,每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

<ol class="dp-j"><li class="alt"><span><span>$arr=array(</span><span class="number">1</span><span>,</span><span class="number">43</span><span>,</span><span class="number">54</span><span>,</span><span class="number">62</span><span>,</span><span class="number">21</span><span>,</span><span class="number">66</span><span>,</span><span class="number">32</span><span>,</span><span class="number">78</span><span>,</span><span class="number">36</span><span>,</span><span class="number">76</span><span>,</span><span class="number">39</span><span>);   </span></span></li><li><span>function bubbleSort($arr) </span></li><li class="alt"><span>{   </span></li><li><span>  $len=count($arr); </span></li><li class="alt"><span>  <span class="comment">//该层循环控制 需要冒泡的轮数</span><span> </span></span></li><li><span>  <span class="keyword">for</span><span>($i=</span><span class="number">1</span><span>;$i<$len;$i++) </span></span></li><li class="alt"><span>  { <span class="comment">//该层循环用来控制每轮 冒出一个数 需要比较的次数</span><span> </span></span></li><li><span>    <span class="keyword">for</span><span>($k=</span><span class="number">0</span><span>;$k<$len-$i;$k++) </span></span></li><li class="alt"><span>    { </span></li><li><span>       <span class="keyword">if</span><span>($arr[$k]>$arr[$k+</span><span class="number">1</span><span>]) </span></span></li><li class="alt"><span>        { </span></li><li><span>            $tmp=$arr[$k+<span class="number">1</span><span>]; </span></span></li><li class="alt"><span>            $arr[$k+<span class="number">1</span><span>]=$arr[$k]; </span></span></li><li><span>            $arr[$k]=$tmp; </span></li><li class="alt"><span>        } </span></li><li><span>    } </span></li><li class="alt"><span>  } </span></li><li><span>  <span class="keyword">return</span><span> $arr; </span></span></li><li class="alt"><span>} </span></li></ol>
Copy after login

2. 选择排序

思路分析:在要排序的一组数中,选出最小的一个数与第一个位置的数交换。然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

<ol class="dp-j"><li class="alt"><span><span>function selectSort($arr) { </span></span></li><li><span><span class="comment">//双重循环完成,外层控制轮数,内层控制比较次数</span><span> </span></span></li><li class="alt"><span>$len=count($arr); </span></li><li><span><span class="keyword">for</span><span>($i=</span><span class="number">0</span><span>; $i<$len-</span><span class="number">1</span><span>; $i++) { </span></span></li><li class="alt"><span><span class="comment">//先假设最小的值的位置</span><span> </span></span></li><li><span>$p = $i; </span></li><li class="alt"><span> </span></li><li><span><span class="keyword">for</span><span>($j=$i+</span><span class="number">1</span><span>; $j<$len; $j++) { </span></span></li><li class="alt"><span><span class="comment">//$arr[$p] 是当前已知的最小值</span><span> </span></span></li><li><span><span class="keyword">if</span><span>($arr[$p] > $arr[$j]) { </span></span></li><li class="alt"><span><span class="comment">//比较,发现更小的,记录下最小值的位置;并且在下次比较时采用已知的最小值进行比较。</span><span> </span></span></li><li><span>$p = $j; </span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span><span class="comment">//已经确定了当前的最小值的位置,保存到$p中。如果发现最小值的位置与当前假设的位置$i不同,则位置互换即可。</span><span> </span></span></li><li><span><span class="keyword">if</span><span>($p != $i) { </span></span></li><li class="alt"><span>$tmp = $arr[$p]; </span></li><li><span>$arr[$p] = $arr[$i]; </span></li><li class="alt"><span>$arr[$i] = $tmp; </span></li><li><span>} </span></li><li class="alt"><span>} </span></li><li><span><span class="comment">//返回最终结果</span><span> </span></span></li><li class="alt"><span><span class="keyword">return</span><span> $arr; </span></span></li><li><span>} </span></li></ol>
Copy after login




Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!