mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
32 lines
548 B
PHP
32 lines
548 B
PHP
<?php
|
|
|
|
/**
|
|
* Bubble Sort
|
|
*
|
|
* @param array $array
|
|
* @return array
|
|
*/
|
|
function bubbleSort($array)
|
|
{
|
|
$length = count($array);
|
|
|
|
for ($i = $length; $i > 0; $i--) {
|
|
$swapped = true;
|
|
|
|
for ($j = 0; $j < $i - 1; $j++) {
|
|
if ($array[$j] > $array[$j + 1]) {
|
|
$temp = $array[$j];
|
|
$array[$j] = $array[$j + 1];
|
|
$array[$j + 1] = $temp;
|
|
$swapped = false;
|
|
}
|
|
}
|
|
|
|
if ($swapped) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $array;
|
|
}
|