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.c
Replay: real traced execution (multi-file project)
#include <stdio.h>

int main(void) {
    int arr[] = {1, 2, 3, 4, 5, 6, 7};
    size_t n = sizeof(arr) / sizeof(arr[0]);
    size_t left = 0;
    size_t right = n - 1;
    while (left < right) {
        int tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
        left = left + 1;
        right = right - 1;
    }
    printf("[");
    for (size_t i = 0; i < n; ++i) {
        if (i > 0) printf(", ");
        printf("%d", arr[i]);
    }
    printf("]\n");
    return 0;
}
  1. arr ← [1, 2, 3, 4, 5, 6, 7]

    3int main(void) {4    int arr[] = {1, 2, 3, 4, 5, 6, 7};5    size_t n = sizeof(arr) / sizeof(arr[0]);
    values this step[1, 2, 3, 4, 5, 6, 7]arr
  2. n ← 7

    4int arr[] = {1, 2, 3, 4, 5, 6, 7};5size_t n = sizeof(arr) / sizeof(arr[0]);6size_t left = 0;
    values this step7n[1, 2, 3, 4, 5, 6, 7]arr
  3. left ← 0

    5size_t n = sizeof(arr) / sizeof(arr[0]);6size_t left = 0;7size_t right = n - 1;
    values this step0left7n
  4. right ← 6

    6size_t left = 0;7size_t right = n - 1;8while (left < right) {
    values this step6right7n0left
  5. arr ← [7, 2, 3, 4, 5, 6, 1]

    10arr[left] = arr[right];11arr[right] = tmp;12left = left + 1;
    values this step[1, 2, 3, 4, 5, 6, 7] [7, 2, 3, 4, 5, 6, 1]arr0left6right
  6. left ← 1

    11arr[right] = tmp;12left = left + 1;13right = right - 1;
    values this step0 1left
  7. right ← 5

    12    left = left + 1;13    right = right - 1;14}
    values this step6 5right
  8. arr ← [7, 6, 3, 4, 5, 2, 1]

    10arr[left] = arr[right];11arr[right] = tmp;12left = left + 1;
    values this step[7, 2, 3, 4, 5, 6, 1] [7, 6, 3, 4, 5, 2, 1]arr1left5right
  9. left ← 2

    11arr[right] = tmp;12left = left + 1;13right = right - 1;
    values this step1 2left
  10. right ← 4

    12    left = left + 1;13    right = right - 1;14}
    values this step5 4right
  11. arr ← [7, 6, 5, 4, 3, 2, 1]

    10arr[left] = arr[right];11arr[right] = tmp;12left = left + 1;
    values this step[7, 6, 3, 4, 5, 2, 1] [7, 6, 5, 4, 3, 2, 1]arr2left4right
  12. left ← 3

    11arr[right] = tmp;12left = left + 1;13right = right - 1;
    values this step2 3left
  13. right ← 3

    12    left = left + 1;13    right = right - 1;14}
    values this step4 3right
  14. while (left < right)

    7size_t right = n - 1;8while (left < right) {9    int tmp = arr[left];
    values this step[7, 6, 5, 4, 3, 2, 1]arr3left3right

Complexity

  • Time: O(n)
  • Space: O(1)

Implementation notes

  • C: use a temporary int tmp to swap two slots. C has no built-in reverse helper, so the lesson stays directly on the two-pointer walk.
  • size_t left and size_t right mirror sizeof's return type and the size discipline used by n; the left < right guard handles the meet-in-the-middle exit honestly for the odd-length canonical input.
  • The replay shows both left and right, the values about to be swapped, and the array contents after the swap. The loop-exit frame is the moment the pointers meet.