-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.php
More file actions
47 lines (43 loc) · 1.33 KB
/
bubble.php
File metadata and controls
47 lines (43 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/**
* Created by PhpStorm.
* User: murat
* Date: 04.12.18
* Time: 18:15
*/
//Asymptotics is in worst case – O(n^2), in best case – O(n).
class Bubble
{
public static function sortWithForeach(&$array)
{
foreach ($array as $firstKey => $item) {
foreach ($array as $secondKey => $nextItem) {
if (isset($array[$secondKey + 1])) {
if ($array[$secondKey] > $array[$secondKey + 1]) {
list($array[$secondKey], $array[$secondKey + 1]) = array($array[$secondKey + 1], $array[$secondKey]);
continue;
}
}
}
}
}
public static function sortWithFor(&$array)
{
$size = count($array) - 1;
for ($i = 0; $i < $size; $i++) {
for ($j = 0; $j < $size - $i; $j++) {
$k = $j + 1;
if ($array[$k] < $array[$j]) {
list($array[$j], $array[$k]) = array($array[$k], $array[$j]);
}
}
}
return $array;
}
}
$array = [156, 0, 3, 1, 9, 2, 5, 6, 8, 0, 100, 3, 1, 9, 2, 5, 6, 8, 7, 7, 7, 7, 7];
Bubble::sortWithFor($array);
print_r($array);
$array = [156, 0, 3, 1, 9, 2, 5, 6, 8, 0, 100, 3, 1, 9, 2, 5, 6, 8, 7, 7, 7, 7, 7];
Bubble::sortWithFor($array);
print_r($array);