0
0
DSA Pythonprogramming

Array Reversal Techniques in DSA Python

Choose your learning style9 modes available
Mental Model
Reversing an array means flipping its order so the last element becomes first and the first becomes last.
Analogy: Imagine a row of people standing in line. Reversing the array is like asking them to turn around and stand in the opposite order.
Index: 0   1   2   3   4
Array: [1] -> [2] -> [3] -> [4] -> [5]
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5]
Goal: Reverse the array so it becomes [5, 4, 3, 2, 1]
Step 1: Swap first element (1) with last element (5)
[5] -> [2] -> [3] -> [4] -> [1]
Why: Swap ends to start reversing the order
Step 2: Swap second element (2) with second last element (4)
[5] -> [4] -> [3] -> [2] -> [1]
Why: Continue swapping pairs moving inward
Step 3: Middle element (3) stays the same because it's in the center
[5] -> [4] -> [3] -> [2] -> [1]
Why: No need to swap middle element in odd-length array
Result:
[5] -> [4] -> [3] -> [2] -> [1]
Annotated Code
DSA Python
class ArrayReversal:
    def __init__(self, arr):
        self.arr = arr

    def reverse(self):
        left = 0
        right = len(self.arr) - 1
        while left < right:
            # Swap elements at left and right
            self.arr[left], self.arr[right] = self.arr[right], self.arr[left]
            left += 1  # Move left pointer inward
            right -= 1  # Move right pointer inward

    def __str__(self):
        return ' -> '.join(str(x) for x in self.arr) + ' -> null'

# Driver code
array = [1, 2, 3, 4, 5]
reversal = ArrayReversal(array)
print("Before reversal:")
print(reversal)
reversal.reverse()
print("After reversal:")
print(reversal)
while left < right:
loop until pointers meet or cross to cover all pairs
self.arr[left], self.arr[right] = self.arr[right], self.arr[left]
swap elements at left and right to reverse order
left += 1 # Move left pointer inward
advance left pointer to next element
right -= 1 # Move right pointer inward
move right pointer inward to next element
OutputSuccess
Before reversal: 1 -> 2 -> 3 -> 4 -> 5 -> null After reversal: 5 -> 4 -> 3 -> 2 -> 1 -> null
Complexity Analysis
Time: O(n) because we swap pairs moving inward, touching each element once
Space: O(1) because reversal is done in place without extra storage
vs Alternative: Naive approach creates a new reversed array using extra space O(n), this method reverses in place saving memory
Edge Cases
Empty array []
No swaps happen, array remains empty
DSA Python
while left < right:
Single element array [7]
No swaps happen, single element stays the same
DSA Python
while left < right:
Array with duplicates [2, 2, 3, 3]
Duplicates are swapped normally, order reversed correctly
DSA Python
self.arr[left], self.arr[right] = self.arr[right], self.arr[left]
When to Use This Pattern
When you need to reverse the order of elements in a list or array efficiently, use the two-pointer swap technique to reverse in place.
Common Mistakes
Mistake: Swapping elements without moving pointers inward causes infinite loop
Fix: Always increment left and decrement right pointers after swapping
Mistake: Using extra space unnecessarily by creating a new array
Fix: Perform swaps in place to save memory
Summary
Reverses an array by swapping elements from ends moving inward.
Use when you want to reverse the order of elements efficiently without extra space.
The key is swapping pairs from the outside towards the center until pointers meet.