-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbubble-sort-v2.php
More file actions
executable file
·51 lines (44 loc) · 990 Bytes
/
bubble-sort-v2.php
File metadata and controls
executable file
·51 lines (44 loc) · 990 Bytes
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
48
49
50
51
<?php
function bubbleSort(array &$a)
{
$len = count($a) - 1;
$sorted = false;
while (!$sorted) {
$sorted = true;
for ($i = 0; $i < $len; $i++) {
$current = $a[$i];
$next = $a[$i + 1];
if ( $next < $current ) {
$a[$i] = $next;
$a[$i + 1] = $current;
$sorted = false;
}
}
}
}
$myArray = [];
$numberPool = 4096;
// add numbers divisible by 2
for ($x = $numberPool; $x >= 0; $x--) {
if ($x % 2 === 0) {
$myArray[] = $x;
}
}
// add numbers divisible by 3
for ($x = $numberPool; $x >= 0; $x--) {
if ($x % 3 === 0) {
$myArray[] = $x;
}
}
// add numbers divisible by 7
for ($x = $numberPool; $x >= 0; $x--) {
if ($x % 7 === 0) {
$myArray[] = $x;
}
}
$startTime = hrtime(true);
bubbleSort($myArray);
$endTime = hrtime(true);
echo '[PHP] array contains ', count($myArray), ' elements, execution time: ',
($endTime - $startTime) / 1000000, ' ms', PHP_EOL;
// echo print_r($myArray, true), PHP_EOL;