Skip to content

Commit bcb9131

Browse files
PudottaPomminjiegillet
authored andcommitted
Bubble sort in php
Also added PHP to book.json
1 parent dc4660e commit bcb9131

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ Gibus Wearing Brony
5151
Arun Sahadeo
5252
<br>
5353
NIFR91
54+
<br>
55+
Michal Hanajik

book.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@
120120
{
121121
"lang": "crystal",
122122
"name": "Crystal"
123+
},
124+
{
125+
"lang": "php",
126+
"name": "PHP"
123127
}
124-
125128
]
126129
}
127130
}

contents/bubble_sort/bubble_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
4242
[import:3-13, lang:"ruby"](code/ruby/bubble.rb)
4343
{% sample lang="crystal" %}
4444
[import:1-11, lang:"crystal"](code/crystal/bubble.cr)
45+
{% sample lang="php" %}
46+
[import:3-15, lang:"php"](code/php/bubble_sort.php)
4547
{% endmethod %}
4648

4749
... And that's it for the simplest bubble sort method.
@@ -89,6 +91,8 @@ Program.cs
8991
[import, lang:ruby"](code/ruby/bubble.rb)
9092
{% sample lang="crystal" %}
9193
[import, lang:"crystal"](code/crystal/bubble.cr)
94+
{% sample lang="php" %}
95+
[import, lang:"php"](code/php/bubble_sort.php)
9296
{% endmethod %}
9397

9498
<script>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
function bubble_sort(array $arr): array
4+
{
5+
for ($i = 0, $length = count($arr); $i < $length; $i++) {
6+
for ($j = 1; $j < $length; $j++) {
7+
if ($arr[$j - 1] > $arr[$j]) {
8+
$tmp = $arr[$j - 1];
9+
$arr[$j - 1] = $arr[$j];
10+
$arr[$j] = $tmp;
11+
}
12+
}
13+
}
14+
return $arr;
15+
}
16+
17+
$unsorted = [1, 2, 6, 47, 4, 9, 3, 7, 8, 23, 15];
18+
$bubble_sorted = bubble_sort($unsorted);
19+
20+
echo sprintf('Unsorted: %s%s', implode(',', $unsorted), PHP_EOL);
21+
echo sprintf('Sorted: %s%s', implode(',', $bubble_sorted), PHP_EOL);

0 commit comments

Comments
 (0)