Skip to content

Commit 19a929a

Browse files
committed
PHP 代码实现 冒泡排序
1 parent 6954cf8 commit 19a929a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1.bubbleSort.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,28 @@ public class BubbleSort implements IArraySort {
109109
return arr;
110110
}
111111
}
112+
```
113+
114+
## 9. PHP 代码实现
115+
116+
```php
117+
function bubbleSort($arr)
118+
{
119+
$count = count($arr);
120+
if ($count == 0) return false;
121+
// 设置一个空数组 用来接收冒出来的泡
122+
$tmp = [];
123+
// 该层循环控制 需要冒泡的轮数
124+
for ($i = 0; $i < $count; $i++) {
125+
//该层循环用来控制每轮 冒出一个数需要比较的次数
126+
for ($j = 0; $j < $count - 1 - $i; $j++) {
127+
if ($arr[$j] > $arr[$j + 1]) {
128+
$tmp = $arr[$j];
129+
$arr[$j] = $arr[$j + 1];
130+
$arr[$j + 1] = $tmp;
131+
}
132+
}
133+
}
134+
return $arr;
135+
}
112136
```

0 commit comments

Comments
 (0)