Walk two indices toward each other from the ends of the array, swapping at each step. Stops when the indices meet or cross. Demonstrates the two-pointer pattern with the smallest possible state.

Algorithm

Canonical input [1, 2, 3, 4, 5, 6, 7] (odd length, middle element stays put) yields three swap frames and reverses to [7, 6, 5, 4, 3, 2, 1].

two pointers `$left` starts at index `0`, `$right` starts at `count($arr) - 1`. Each loop iteration swaps `$arr[$left]` and `$arr[$right]` and moves the pointers toward each other.

Basic Implementation

basic.php
Replay: real traced execution (multi-file project)
<?php
$arr = [1, 2, 3, 4, 5, 6, 7];
$left = 0;
$right = count($arr) - 1;
while ($left < $right) {
	$tmp = $arr[$left];
	$arr[$left] = $arr[$right];
	$arr[$right] = $tmp;
	$left = $left + 1;
	$right = $right - 1;
}
echo "[" . implode(", ", $arr) . "]\n";
  1. $arr ← [1, 2, 3, 4, 5, 6, 7]

    1<?php2$arr = [1, 2, 3, 4, 5, 6, 7];3$left = 0;
    values this step[1, 2, 3, 4, 5, 6, 7]$arr
  2. $left ← 0

    2$arr = [1, 2, 3, 4, 5, 6, 7];3$left = 0;4$right = count($arr) - 1;
    values this step0$left[1, 2, 3, 4, 5, 6, 7]$arr
  3. $right ← 6

    3$left = 0;4$right = count($arr) - 1;5while ($left < $right) {
    values this step6$right0$left
  4. $arr ← [7, 2, 3, 4, 5, 6, 1]

    6$tmp = $arr[$left];7$arr[$left] = $arr[$right];8$arr[$right] = $tmp;
    values this step[1, 2, 3, 4, 5, 6, 7] [7, 2, 3, 4, 5, 6, 1]$arr0$left6$right
  5. $left ← 1

    8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;
    values this step0 1$left
  6. $right ← 5

    9	$left = $left + 1;10	$right = $right - 1;11}
    values this step6 5$right
  7. $arr ← [7, 6, 3, 4, 5, 2, 1]

    6$tmp = $arr[$left];7$arr[$left] = $arr[$right];8$arr[$right] = $tmp;
    values this step[7, 2, 3, 4, 5, 6, 1] [7, 6, 3, 4, 5, 2, 1]$arr1$left5$right
  8. $left ← 2

    8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;
    values this step1 2$left
  9. $right ← 4

    9	$left = $left + 1;10	$right = $right - 1;11}
    values this step5 4$right
  10. $arr ← [7, 6, 5, 4, 3, 2, 1]

    6$tmp = $arr[$left];7$arr[$left] = $arr[$right];8$arr[$right] = $tmp;
    values this step[7, 6, 3, 4, 5, 2, 1] [7, 6, 5, 4, 3, 2, 1]$arr2$left4$right
  11. $left ← 3

    8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;
    values this step2 3$left
  12. $right ← 3

    9	$left = $left + 1;10	$right = $right - 1;11}
    values this step4 3$right
  13. while ($left < $right)

    4$right = count($arr) - 1;5while ($left < $right) {6	$tmp = $arr[$left];
    values this step[7, 6, 5, 4, 3, 2, 1]$arr3$left3$right

Complexity

  • Time: O(n)
  • Space: O(1)

Implementation notes

  • PHP: explicit three-line $tmp = $arr[$left]; $arr[$left] = $arr[$right]; $arr[$right] = $tmp; swap keeps the move visible. The stdlib array_reverse($arr) (or $arr = array_reverse($arr);) would hide the lesson entirely, and [$arr[$left], $arr[$right]] = [$arr[$right], $arr[$left]]; list destructuring would collapse the swap into a single frame.
  • $left = 0 and $right = count($arr) - 1 use plain int indices; count() returns the fixed length of the canonical array.
  • The replay distinguishes swap frames from pointer-advance frames so the viewer can see $left and $right converge.