码迷,mamicode.com
首页 > 编程语言 > 详细

PHP 冒泡排序

时间:2017-03-02 21:28:24      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:nbsp   color   .com   img   时间复杂度   imp   count   ble   sort   

1. 冒泡排序 -- Bubble Insertion Sort

2. 时间复杂度 : O(n ^2)

3.适用: 低效的排序方法

 1 <?php
 2 $arr = [3,4,5,1,11,9,27,27,18,20];
 3 
 4 function bubbleSort(array &$arr)
 5 {
 6     $len = count($arr);
 7 
 8     for($i = 0;$i < $len - 1; ++$i)
 9     {
10         for($j = 0;$j < $len - $i - 1; ++$j)
11         {
12             if($arr[$j] > $arr[$j + 1])
13             {
14                 $tmp = $arr[$j+1];
15                 $arr[$j+1] = $arr[$j];
16                 $arr[$j] = $tmp;
17             }
18         }
19     }
20 }
21 
22 bubbleSort($arr);
23 echo implode(‘,‘,$arr);

 

运行结果:

技术分享

 

PHP 冒泡排序

标签:nbsp   color   .com   img   时间复杂度   imp   count   ble   sort   

原文地址:http://www.cnblogs.com/jingjingdidunhe/p/6492518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!