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";
  1. 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]arr
  2. left ← 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]arr
  3. right ← 6

    3my $left = 0;4my $right = scalar(@arr) - 1;5while ($left < $right) {
    values this step6right0left
  4. arr ← [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]arr0left6right
  5. left ← 1

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

    9	$left = $left + 1;10	$right = $right - 1;11}
    values this step6 5right
  7. arr ← [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]arr1left5right
  8. left ← 2

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

    9	$left = $left + 1;10	$right = $right - 1;11}
    values this step5 4right
  10. arr ← [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]arr2left4right
  11. left ← 3

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

    9	$left = $left + 1;10	$right = $right - 1;11}
    values this step4 3right
  13. while ($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] = $tmp swap keeps the move visible. The stdlib reverse @arr would 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 = 0 and $right = scalar(@arr) - 1 use plain integer indices; the $left < $right guard 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 $left and $right converge.