-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHP.php
36 lines (29 loc) · 732 Bytes
/
PHP.php
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
/**
* Author: Ozuru Icheka
* GitHub: https://github.com/icheka
* Email: rhemafortune@gmail.com
*/
class Sort {
protected sort(array $arr, int $idx1, int $idx2) {
if ($arr[idx1] > $arr[idx2]) {
$tmp = $arr[idx1];
$arr[idx1] = $arr[idx2];
$arr[idx2] = $tmp;
}
return arr;
}
public function bubbleSort(array $arr) {
for ($i = 0; $i < count(arr) - 1; $i++) { //perform the swap operation on each item in the array
for ($j = 0; $j < count(arr) - 1; $j++) { //get each item and its near-right neighbor
$arr = $this->swap($arr, $j, $j + 1); //perform the swap
}
}
return $arr;
}
}
function main() {
$arr = array(3,5,1,4,8,7,9);
$sort = new Sort();
print_r($sort->bubbleSort($arr));
}
main();