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; +} ```