Skip to content
thesarfo

Reference

Reversing an Array Recursively

Reversing an array by swapping the first and last elements, then recursing on the inner subarray.

views 0

Reversing can also be done recursively by swapping the first and last elements and then calling the function again for the inner part of the array.

void reverse(int[] arr, int start, int end) {
if (start >= end) return;
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverse(arr, start + 1, end - 1);
}

The base case occurs when the two indices cross, meaning the array has been completely reversed.