Bird
0
0
DSA Cprogramming

Array Reversal Techniques in DSA C

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 books on a shelf. Reversing the array is like taking the books from both ends and swapping them until you meet in the middle.
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 inward to reverse the array
Step 3: Middle element (3) stays the same as it is the center
[5] -> [4] -> [3] -> [2] -> [1]
Why: No swap needed for middle element in odd length array
Result:
[5] -> [4] -> [3] -> [2] -> [1]
Annotated Code
DSA C
#include <stdio.h>

void reverseArray(int arr[], int n) {
    int start = 0;
    int end = n - 1;
    while (start < end) {
        // Swap elements at start and end
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        // Move pointers inward
        start++;
        end--;
    }
}

void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%d", arr[i]);
        if (i < n - 1) printf(" -> ");
    }
    printf("\n");
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("Original array:\n");
    printArray(arr, n);
    reverseArray(arr, n);
    printf("Reversed array:\n");
    printArray(arr, n);
    return 0;
}
while (start < end) {
loop until pointers meet or cross to cover all swaps
int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp;
swap elements at start and end positions
start++; end--;
move pointers inward to next pair
OutputSuccess
Original array: 1 -> 2 -> 3 -> 4 -> 5 Reversed array: 5 -> 4 -> 3 -> 2 -> 1
Complexity Analysis
Time: O(n) because each element is visited once during swapping
Space: O(1) because reversal is done in place without extra storage
vs Alternative: Compared to creating a new reversed array which uses O(n) extra space, this method is more space efficient
Edge Cases
empty array
function does nothing and array remains empty
DSA C
while (start < end) {
array with one element
no swaps occur, array stays the same
DSA C
while (start < end) {
array with even number of elements
all pairs swapped until pointers cross, full reversal achieved
DSA C
while (start < end) {
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 causing infinite loop
Fix: Increment start and decrement end pointers inside the loop after swapping
Mistake: Swapping elements incorrectly by overwriting without temporary variable
Fix: Use a temporary variable to hold one element during swap
Summary
Reverses the order of elements in an array by swapping pairs from ends inward.
Use when you want to reverse an array efficiently without extra space.
The key is swapping elements from start and end moving inward until they meet.