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 `scalar(@arr) - 1`. Each loop iteration swaps `$arr[$left]` and `$arr[$right]` and moves the pointers toward each other.
Basic Implementation
basic.pl
Replay: real traced execution (multi-file project)
use strict; use warnings;
my @arr = (1, 2, 3, 4, 5, 6, 7);
my $left = 0;
my $right = scalar(@arr) - 1;
while ($left < $right) {
my $tmp = $arr[$left];
$arr[$left] = $arr[$right];
$arr[$right] = $tmp;
$left = $left + 1;
$right = $right - 1;
}
print "[" . join(", ", @arr) . "]\n";
arr ← [1, 2, 3, 4, 5, 6, 7]
1use strict; use warnings;2my @arr = (1, 2, 3, 4, 5, 6, 7);3my $left = 0;values this step[1, 2, 3, 4, 5, 6, 7]arrleft ← 0
2my @arr = (1, 2, 3, 4, 5, 6, 7);3my $left = 0;4my $right = scalar(@arr) - 1;values this step0left[1, 2, 3, 4, 5, 6, 7]arrright ← 6
3my $left = 0;4my $right = scalar(@arr) - 1;5while ($left < $right) {values this step6right0leftarr ← [7, 2, 3, 4, 5, 6, 1]
6my $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]arr0left6rightleft ← 1
8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;values this step0 → 1leftright ← 5
9 $left = $left + 1;10 $right = $right - 1;11}values this step6 → 5rightarr ← [7, 6, 3, 4, 5, 2, 1]
6my $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]arr1left5rightleft ← 2
8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;values this step1 → 2leftright ← 4
9 $left = $left + 1;10 $right = $right - 1;11}values this step5 → 4rightarr ← [7, 6, 5, 4, 3, 2, 1]
6my $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]arr2left4rightleft ← 3
8$arr[$right] = $tmp;9$left = $left + 1;10$right = $right - 1;values this step2 → 3leftright ← 3
9 $left = $left + 1;10 $right = $right - 1;11}values this step4 → 3rightwhile ($left < $right)
4my $right = scalar(@arr) - 1;5while ($left < $right) {6 my $tmp = $arr[$left];values this step[7, 6, 5, 4, 3, 2, 1]arr3left3right
Complexity
- Time: O(n)
- Space: O(1)
Implementation notes
- Perl: explicit three-line
my $tmp = $arr[$left]; $arr[$left] = $arr[$right]; $arr[$right] = $tmpswap keeps the move visible. The stdlibreverse @arrwould hide the lesson entirely (and returns a new list rather than mutating in place), and@arr[$left, $right] = @arr[$right, $left](list-slice assignment) would collapse the swap into a single frame. $left = 0and$right = scalar(@arr) - 1use plain integer indices; the$left < $rightguard handles the meet-in-the-middle exit honestly for the odd-length canonical input.- The replay distinguishes swap frames from pointer-advance frames so
the viewer can see
$leftand$rightconverge.