数据结构&算法(PHP描述) 冒泡排序 bubble sort

简介:这是数据结构&算法(PHP描述) 冒泡排序 bubble sort的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=340629' scrolling='no'>
1 <?php
2 /**
3 * 冒泡排序 bubble sort
4 *
5 * 原理:多次循环进行比较,每次比较时将最大数移动到最上面。每次循环时,找出剩余变量里的最大值,然后减小查询范围。这样经过多次循环以后,就完成了对这个数组的排序
6 */
7 function sort_bubble($list)
8 {
9 $len = count($list);
10 if(empty($len)) return $list;
11
12 for($i = 0;$i < $len; $i++)
13 {
14 for($j = $i + 1; $j < $len; $j++)
15 {
16 $flag = '';
17 if($list[$i] > $list[$j]) // 从小到大
18 //if($list[$i] < $list[$j]) // 从大到小
19 {
20 $tmp = $list[$i];
21 $list[$i] = $list[$j];
22 $list[$j] = $tmp;
23
24 $flag = " change";
25 }
26 echo implode(',',$list).$flag."<br/>";
27 }
28 echo "-------------------------<br/>";
29 }
30 return $list;
31 }
32
33 $list = array(4,3,2,1,5,7,3,7);
34 $list = sort_bubble($list);

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/340629.html pageNo:7
原文地址:https://www.cnblogs.com/ooooo/p/2245227.html