<?php
function bubbleSort($arr) {
$n = count($arr);
// Traverse through all array elements
for ($i = 0; $i < $n - 1; $i++) {
// Last i elements are already in place
for ($j = 0; $j < $n - $i - 1; $j++) {
// Swap if the element found is greater than the next element
if ($arr[$j] > $arr[$j + 1]) {
$temp = $arr[$j];
$arr[$j] = $arr[$j + 1];
$arr[$j + 1] = $temp;
}
}
}
return $arr;
}
// Test the function
$arr = [64, 34, 25, 12, 22, 11, 90];
echo "Original Array: \n";
print_r($arr);
$arr = bubbleSort($arr);
echo "Sorted Array: \n";
print_r($arr);
?>
Comments
Post a Comment