Repeatedly walk the array comparing adjacent pairs and swapping any that are out of order. After pass k, the k largest elements are in their final positions at the end. Stop early when a full pass makes zero swaps.

Algorithm

Canonical input [5, 1, 4, 2, 8] finishes after three passes: two with swaps, then a clean pass that triggers the early exit. Final array [1, 2, 4, 5, 8].

adjacent-pair compare and swap Inner loop walks `$j` from `0` to `$n - $i - 2` comparing `$arr[$j]` and `$arr[$j + 1]`.
early exit A `$swapped` flag set `false` at the start of each pass. If no swap happened, flip a `$done` flag and break out of the outer loop.

Basic Implementation

basic.php
Replay: real traced execution (multi-file project)
<?php
$arr = [5, 1, 4, 2, 8];
$n = count($arr);
$i = 0;
$done = false;
while ($i < $n - 1 && !$done) {
	$swapped = false;
	$j = 0;
	while ($j < $n - $i - 1) {
		if ($arr[$j] > $arr[$j + 1]) {
			$tmp = $arr[$j];
			$arr[$j] = $arr[$j + 1];
			$arr[$j + 1] = $tmp;
			$swapped = true;
		}
		$j = $j + 1;
	}
	if (!$swapped) {
		$done = true;
	}
	$i = $i + 1;
}
echo "[" . implode(", ", $arr) . "]\n";
  1. $arr ← [5, 1, 4, 2, 8]

    1<?php2$arr = [5, 1, 4, 2, 8];3$n = count($arr);
    values this step[5, 1, 4, 2, 8]$arr
  2. $n ← 5

    2$arr = [5, 1, 4, 2, 8];3$n = count($arr);4$i = 0;
    values this step5$n[5, 1, 4, 2, 8]$arr
  3. $swapped ← false

    6while ($i < $n - 1 && !$done) {7	$swapped = false;8	$j = 0;
    values this stepfalse$swapped
  4. $arr[$j] > $arr[$j+1] ← true

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this steptrue$arr[$j] > $arr[$j+1][5, 1, 4, 2, 8]$arr0$j5$arr[$j]1$arr[$j+1]
  5. $arr ← [1, 5, 4, 2, 8], $swapped ← true

    11$tmp = $arr[$j];12$arr[$j] = $arr[$j + 1];13$arr[$j + 1] = $tmp;
    values this step[5, 1, 4, 2, 8] [1, 5, 4, 2, 8]$arrtrue$swapped
  6. $arr[$j] > $arr[$j+1] ← true

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this steptrue$arr[$j] > $arr[$j+1][1, 5, 4, 2, 8]$arr1$j5$arr[$j]4$arr[$j+1]
  7. $arr ← [1, 4, 5, 2, 8], $swapped ← true

    11$tmp = $arr[$j];12$arr[$j] = $arr[$j + 1];13$arr[$j + 1] = $tmp;
    values this step[1, 5, 4, 2, 8] [1, 4, 5, 2, 8]$arrtrue$swapped
  8. $arr[$j] > $arr[$j+1] ← true

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this steptrue$arr[$j] > $arr[$j+1][1, 4, 5, 2, 8]$arr2$j5$arr[$j]2$arr[$j+1]
  9. $arr ← [1, 4, 2, 5, 8], $swapped ← true

    11$tmp = $arr[$j];12$arr[$j] = $arr[$j + 1];13$arr[$j + 1] = $tmp;
    values this step[1, 4, 5, 2, 8] [1, 4, 2, 5, 8]$arrtrue$swapped
  10. $arr[$j] > $arr[$j+1] ← false

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this stepfalse$arr[$j] > $arr[$j+1][1, 4, 2, 5, 8]$arr3$j5$arr[$j]8$arr[$j+1]
  11. $swapped ← false

    6while ($i < $n - 1 && !$done) {7	$swapped = false;8	$j = 0;
    values this stepfalse$swapped
  12. $arr[$j] > $arr[$j+1] ← false

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this stepfalse$arr[$j] > $arr[$j+1][1, 4, 2, 5, 8]$arr0$j1$arr[$j]4$arr[$j+1]
  13. $arr[$j] > $arr[$j+1] ← true

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this steptrue$arr[$j] > $arr[$j+1][1, 4, 2, 5, 8]$arr1$j4$arr[$j]2$arr[$j+1]
  14. $arr ← [1, 2, 4, 5, 8], $swapped ← true

    11$tmp = $arr[$j];12$arr[$j] = $arr[$j + 1];13$arr[$j + 1] = $tmp;
    values this step[1, 4, 2, 5, 8] [1, 2, 4, 5, 8]$arrtrue$swapped
  15. $arr[$j] > $arr[$j+1] ← false

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this stepfalse$arr[$j] > $arr[$j+1][1, 2, 4, 5, 8]$arr2$j4$arr[$j]5$arr[$j+1]
  16. $swapped ← false

    6while ($i < $n - 1 && !$done) {7	$swapped = false;8	$j = 0;
    values this stepfalse$swapped
  17. $arr[$j] > $arr[$j+1] ← false

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this stepfalse$arr[$j] > $arr[$j+1][1, 2, 4, 5, 8]$arr0$j1$arr[$j]2$arr[$j+1]
  18. $arr[$j] > $arr[$j+1] ← false

    9while ($j < $n - $i - 1) {10	if ($arr[$j] > $arr[$j + 1]) {11		$tmp = $arr[$j];
    values this stepfalse$arr[$j] > $arr[$j+1][1, 2, 4, 5, 8]$arr1$j2$arr[$j]4$arr[$j+1]
  19. $done ← true

    18if (!$swapped) {19	$done = true;20}
    values this steptrue$donefalse$swapped
  20. stdout ← [1, 2, 4, 5, 8]

    22}23echo "[" . implode(", ", $arr) . "]\n";
    values this step[1, 2, 4, 5, 8]stdout[1, 2, 4, 5, 8]$arr

Complexity

  • Time: O(n^2) worst and average; O(n) best (already sorted with early exit)
  • Space: O(1)
  • Stable: yes

Implementation notes

  • PHP: explicit while loops with $i, $j, $done, and $swapped so the early-exit flow stays visible. The stdlib sort($arr) would hide the comparison-and-swap the lesson is teaching, and a break 2; inside a foreach would collapse the $done flag into a control-flow shortcut.
  • The explicit $tmp = $arr[$j]; $arr[$j] = $arr[$j+1]; $arr[$j+1] = $tmp; three-line swap keeps the move visible without leaning on list destructuring like [$arr[$j], $arr[$j+1]] = [$arr[$j+1], $arr[$j]];.
  • The replay distinguishes compare frames from swap frames so the moving pivot value is visible. The pass number and $swapped flag appear in the trace.