2022-07-23 23:37:36 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bubble Sort
|
|
|
|
*
|
|
|
|
* @param array $array
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-10-04 21:36:11 +03:30
|
|
|
function bubbleSort($array)
|
|
|
|
{
|
2022-07-23 23:37:36 -06:00
|
|
|
$length = count($array);
|
|
|
|
|
|
|
|
for ($i = $length; $i > 0; $i--) {
|
|
|
|
$swapped = true;
|
|
|
|
|
2023-10-04 21:36:11 +03:30
|
|
|
for ($j = 0; $j < $i - 1; $j++) {
|
2022-07-23 23:37:36 -06:00
|
|
|
if ($array[$j] > $array[$j + 1]) {
|
|
|
|
$temp = $array[$j];
|
|
|
|
$array[$j] = $array[$j + 1];
|
|
|
|
$array[$j + 1] = $temp;
|
|
|
|
$swapped = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 21:36:11 +03:30
|
|
|
if ($swapped) {
|
|
|
|
break;
|
|
|
|
}
|
2022-07-23 23:37:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|