Since we are assuming the unsorted number will be in a list, we can use a PHP array to represent the list of unsorted numbers. Since the array has both index and values, we can utilize the array to easily iterate through each item, based on position, and swap them where it is applicable. The code will look like this, based on our pseudocodes:
function bubbleSort(array $arr): array { $len = count($arr); for ($i = 0; $i < $len; $i++) { for ($j = 0; $j < $len - 1; $j++) { if ($arr[$j] > $arr[$j + 1]) { $tmp = $arr[$j + 1]; $arr[$j + 1] = $arr[$j]; $arr[$j] = $tmp; } } } return $arr; }
As we can see, we are using two for loops to iterate each item and comparing with the rest of the items. The swapping ...