From 19a929aa48b9d3cd2774efbdb6ae6d07cb1396fb Mon Sep 17 00:00:00 2001 From: Tinywan <756684177@qq.com> Date: Fri, 30 Mar 2018 10:00:33 +0800 Subject: [PATCH] =?UTF-8?q?PHP=20=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0=20?= =?UTF-8?q?=E5=86=92=E6=B3=A1=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.bubbleSort.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 621314f..2a35116 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -109,4 +109,28 @@ public class BubbleSort implements IArraySort { return arr; } } +``` + +## 9. PHP 代码实现 + +```php +function bubbleSort($arr) +{ + $count = count($arr); + if ($count == 0) return false; + // 设置一个空数组 用来接收冒出来的泡 + $tmp = []; + // 该层循环控制 需要冒泡的轮数 + for ($i = 0; $i < $count; $i++) { + //该层循环用来控制每轮 冒出一个数需要比较的次数 + for ($j = 0; $j < $count - 1 - $i; $j++) { + if ($arr[$j] > $arr[$j + 1]) { + $tmp = $arr[$j]; + $arr[$j] = $arr[$j + 1]; + $arr[$j + 1] = $tmp; + } + } + } + return $arr; +} ```