-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubblesort.php
More file actions
30 lines (26 loc) · 766 Bytes
/
bubblesort.php
File metadata and controls
30 lines (26 loc) · 766 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
<?php
function bubbleSort($array) {
$n = count($array);
// Loop para percorrer todo o array
for ($i = 0; $i < $n - 1; $i++) {
// Loop para comparar os elementos adjacentes
for ($j = 0; $j < $n - $i - 1; $j++) {
// Verifica se o elemento atual é maior que o próximo
if ($array[$j] > $array[$j + 1]) {
// Troca os elementos de lugar
$temp = $array[$j];
$array[$j] = $array[$j + 1];
$array[$j + 1] = $temp;
}
}
}
return $array;
}
// Exemplo de uso
$array = [64, 34, 25, 12, 22, 11, 90];
echo "Array original: ";
print_r($array);
$arrayOrdenado = bubbleSort($array);
echo "Array ordenado: ";
print_r($arrayOrdenado);
?>