Arrays and Iteration
Reverse Array In Place (Two Pointers)
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";
$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$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$right ← 6
3$left = 0;4$right = count($arr) - 1;5while ($left < $right) {values this step6$right0$left$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$left ← 1
8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;values this step0 → 1$left$right ← 5
9 $left = $left + 1;10 $right = $right - 1;11}values this step6 → 5$right$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$left ← 2
8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;values this step1 → 2$left$right ← 4
9 $left = $left + 1;10 $right = $right - 1;11}values this step5 → 4$right$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$left ← 3
8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;values this step2 → 3$left$right ← 3
9 $left = $left + 1;10 $right = $right - 1;11}values this step4 → 3$rightwhile ($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 stdlibarray_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 = 0and$right = count($arr) - 1use plainintindices;count()returns the fixed length of the canonical array.- The replay distinguishes swap frames from pointer-advance frames so the
viewer can see
$leftand$rightconverge.