-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbubble.php
49 lines (40 loc) · 1.13 KB
/
bubble.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
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
// bubble sort algorithm in php
function bubbleSort() {
$array = func_get_args();
$count = func_num_args();
for($i = 0; $i < $count; $i++) {
for($j = 0; $j < $count - 1; $j++) {
if($array[$j] > $array[$j + 1]) {
$temp = $array[$j];
$array[$j] = $array[$j + 1];
$array[$j + 1] = $temp;
}
}
}
return $array;
}
$sorted_arr = bubbleSort(9, 6, 4, 8, 3, -7, 2, 1, 5);
error_log(print_r($sorted_arr, true));
// select sort algorithm in php
function selectSort() {
$array = func_get_args();
$count = func_num_args();
$min_index = 0;
for($i = 0; $i < $count; $i++) {
$min_index = $i;
for($j = $i + 1; $j < $count; $j++) {
if($array[$j] < $array[$min_index]) {
$min_index = $j;
}
}
if($min_index != $i) {
$temp = $array[$i];
$array[$i] = $array[$min_index];
$array[$min_index] = $temp;
}
}
return $array;
}
$select_arr = selectSort(-9, 6, 4, 8, 3, 7, 2, 1, 5);
error_log(print_r($select_arr, true));