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 `n - 1`. Each loop iteration swaps `arr[left]` and `arr[right]` and moves the pointers toward each other.
Basic Implementation
basic.rs
Replay: real traced execution (multi-file project)
fn main() {
let mut arr = [1, 2, 3, 4, 5, 6, 7];
let mut left = 0usize;
let mut right = arr.len() - 1;
while left < right {
let tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
left = left + 1;
right = right - 1;
}
println!("{:?}", arr);
}
arr ← [1, 2, 3, 4, 5, 6, 7]
1fn main() {2 let mut arr = [1, 2, 3, 4, 5, 6, 7];3 let mut left = 0usize;values this step[1, 2, 3, 4, 5, 6, 7]arrleft ← 0
2let mut arr = [1, 2, 3, 4, 5, 6, 7];3let mut left = 0usize;4let mut right = arr.len() - 1;values this step0left[1, 2, 3, 4, 5, 6, 7]arrright ← 6
3let mut left = 0usize;4let mut right = arr.len() - 1;5while left < right {values this step6right0leftarr ← [7, 2, 3, 4, 5, 6, 1]
6let tmp = arr[left];7arr[left] = arr[right];8arr[right] = tmp;values this step[1, 2, 3, 4, 5, 6, 7] → [7, 2, 3, 4, 5, 6, 1]arr0left6rightleft ← 1
8arr[right] = tmp;9left = left + 1;10right = 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]
6let tmp = arr[left];7arr[left] = arr[right];8arr[right] = tmp;values this step[7, 2, 3, 4, 5, 6, 1] → [7, 6, 3, 4, 5, 2, 1]arr1left5rightleft ← 2
8arr[right] = tmp;9left = left + 1;10right = 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]
6let tmp = arr[left];7arr[left] = arr[right];8arr[right] = tmp;values this step[7, 6, 3, 4, 5, 2, 1] → [7, 6, 5, 4, 3, 2, 1]arr2left4rightleft ← 3
8arr[right] = tmp;9left = left + 1;10right = right - 1;values this step2 → 3leftright ← 3
9 left = left + 1;10 right = right - 1;11}values this step4 → 3rightwhile left < right
4let mut right = arr.len() - 1;5while left < right {6 let tmp = arr[left];values this step[7, 6, 5, 4, 3, 2, 1]arr3left3right
Complexity
- Time: O(n)
- Space: O(1)
Implementation notes
- Rust: explicit three-line
let tmp = ...; arr[left] = arr[right]; arr[right] = tmp;swap keeps the move visible. The standardarr.swap(left, right)andVec::reversewould hide the lesson. let mut left = 0usize;andlet mut right = arr.len() - 1;use plainusizeindices; theleft < rightguard handles the meet-in-the-middle exit honestly for the odd-length canonical input.- The replay shows both
leftandright, the values about to be swapped, and the array contents after the swap. The loop-exit frame is the moment the pointers meet.