未加星标

四种基本排序算法以及PHP实现

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

因为之前没有编程基础,最近才知道排序算法这个概念,原来平时在操作数组的时候是用到“算法”这玩意的。

从网上找了下资料,都一个来源;用 $ arr = array ( 1 , 43 , 54 , 62 , 21 , 66 , 32 , 78 , 36 , 76 , 39 ) ; 做例子排序。

1. 冒泡排序

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

代码实现:

</div>
<div id="crayon-57fd8e9910989465619608-2" class="crayon-line crayon-striped-line"><span class="crayon-t">function</span> <span class="crayon-e">bubbleSort</span><span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-v">arr</span><span class="crayon-sy">)</span></div>
<div id="crayon-57fd8e9910989465619608-3" class="crayon-line"><span class="crayon-sy">{</span><span class="crayon-h"> </span></div>
<div id="crayon-57fd8e9910989465619608-4" class="crayon-line crayon-striped-line"><span class="crayon-h"> </span><span class="crayon-sy">$</span><span class="crayon-v">len</span><span class="crayon-o">=</span><span class="crayon-e">count</span><span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-v">arr</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-57fd8e9910989465619608-5" class="crayon-line"><span class="crayon-h"> </span><span class="crayon-c">//该层循环控制 需要冒泡的轮数</span></div>
<div id="crayon-57fd8e9910989465619608-6" class="crayon-line crayon-striped-line"><span class="crayon-h"> </span><span class="crayon-st">for</span><span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-v">i</span><span class="crayon-o">=</span><span class="crayon-cn">1</span><span class="crayon-sy">;</span><span class="crayon-sy">$</span><span class="crayon-v">i</span><span class="crayon-o"><</span><span class="crayon-sy">$</span><span class="crayon-v">len</span><span class="crayon-sy">;</span><span class="crayon-sy">$</span><span class="crayon-v">i</span><span class="crayon-o">++</span><span class="crayon-sy">)</span></div>
<div id="crayon-57fd8e9910989465619608-7" class="crayon-line"><span class="crayon-h"> </span><span class="crayon-sy">{</span> <span class="crayon-c">//该层循环用来控制每轮 冒出一个数 需要比较的次数</span></div>
<div id="crayon-57fd8e9910989465619608-8" class="crayon-line crayon-striped-line"><span class="crayon-h"> </span><span class="crayon-st">for</span><span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-o">=</span><span class="crayon-cn">0</span><span class="crayon-sy">;</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-o"><</span><span class="crayon-sy">$</span><span class="crayon-v">len</span><span class="crayon-o">-</span><span class="crayon-sy">$</span><span class="crayon-v">i</span><span class="crayon-sy">;</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-o">++</span><span class="crayon-sy">)</span></div>
<div id="crayon-57fd8e9910989465619608-9" class="crayon-line"><span class="crayon-h"> </span><span class="crayon-sy">{</span></div>
<div id="crayon-57fd8e9910989465619608-10" class="crayon-line crayon-striped-line"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-v">arr</span><span class="crayon-sy">[</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-sy">]</span><span class="crayon-o">></span><span class="crayon-sy">$</span><span class="crayon-v">arr</span><span class="crayon-sy">[</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-o">+</span><span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-sy">)</span></div>
<div id="crayon-57fd8e9910989465619608-11" class="crayon-line"><span class="crayon-h"> </span><span class="crayon-sy">{</span></div>
<div id="crayon-57fd8e9910989465619608-12" class="crayon-line crayon-striped-line"><span class="crayon-h"> </span><span class="crayon-sy">$</span><span class="crayon-v">tmp</span><span class="crayon-o">=</span><span class="crayon-sy">$</span><span class="crayon-v">arr</span><span class="crayon-sy">[</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-o">+</span><span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-sy">;</span></div>
<div id="crayon-57fd8e9910989465619608-13" class="crayon-line"><span class="crayon-h"> </span><span class="crayon-sy">$</span><span class="crayon-v">arr</span><span class="crayon-sy">[</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-o">+</span><span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-o">=</span><span class="crayon-sy">$</span><span class="crayon-v">arr</span><span class="crayon-sy">[</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-sy">]</span><span class="crayon-sy">;</span></div>
<div id="crayon-57fd8e9910989465619608-14" class="crayon-line crayon-striped-line"><span class="crayon-h"> </span><span class="crayon-sy">$</span><span class="crayon-v">arr</span><span class="crayon-sy">[</span><span class="crayon-sy">$</span><span class="crayon-v">k</span><span class="crayon-sy">]</span><span class="crayon-o">=</span><span class="crayon-sy">$</span><span class="crayon-v">tmp</span><span class="crayon-sy">;</span></div>
<div id="crayon-57fd8e9910989465619608-15" class="crayon-line"><span class="crayon-h"> </span><span class="crayon-sy">}</span></div>
<div id="crayon-57fd8e9910989465619608-16" class="crayon-line crayon-striped-line"&g

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: 算法PHP
分页:12
转载请注明
本文标题:四种基本排序算法以及PHP实现
本站链接:http://www.codesec.net/view/482479.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(36)