Reversing an array is a fundamental problem in C programming, often featured in coding interviews and practical applications. This task involves rearranging the elements of an array in reverse order, with the first element becoming the last, the second becoming the second-to-last, and so on. In this article, we will explore three effective methods to write a C Program to Reverse an Array. Each method includes detailed explanations, example code, and output to ensure a clear understanding. You can also refer to our complete guide on Reverse a Number in C and String Reverse in C.
C Program to Reverse an Array: A Complete Guide
Why Reverse an Array in C?
Reversing an array is a versatile operation with applications in algorithms, data manipulation, and problem-solving. It tests your understanding of array handling and basic logic in C programming. Let’s dive into the methods to achieve this.
Method 1) C Program to Reverse an Array Using Two-Pointer Technique
Overview
The two-pointer technique is an efficient method to reverse an array. It swaps elements at the beginning and end of the array and progresses towards the center.
Code Example
#include <stdio.h> void reverseArray(int arr[], int n) { int start = 0, end = n - 1; while (start < end) { // Swap elements int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; // Move pointers start++; end--; } } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); reverseArray(arr, n); printf("Reversed array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Explanation
- We initialize two pointers: one at the start and one at the end of the array.
- We swap the elements at these positions until the pointers meet in the center.
- This method is space-efficient as it works in-place without extra memory.
Output
Reversed array: 5 4 3 2 1
Method 2) C Program to Reverse an Array Using an Auxiliary Array
Overview
This method uses an auxiliary array to store the reversed elements and copies them back to the original array.
Code Example
#include <stdio.h> void reverseArray(int arr[], int n) { int aux[n]; // Copy elements to auxiliary array in reverse order for (int i = 0; i < n; i++) { aux[i] = arr[n - i - 1]; } // Copy back to the original array for (int i = 0; i < n; i++) { arr[i] = aux[i]; } } int main() { int arr[] = {10, 20, 30, 40, 50}; int n = sizeof(arr) / sizeof(arr[0]); reverseArray(arr, n); printf("Reversed array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Explanation
- We create an auxiliary array of the same size.
- We copy elements from the original array into the auxiliary array in reverse order.
- Next, we transfer the reversed data back to the original array.
Output
Reversed array: 50 40 30 20 10
Method 3) C Program to Reverse an Array Using Recursion
Overview
The recursive approach simplifies the logic by breaking the problem into smaller subproblems. It swaps the first and last elements during each recursive call until reversing the entire array.
Code Example
#include <stdio.h> void reverseArrayRecursive(int arr[], int start, int end) { if (start >= end) return; // Swap elements int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; // Recursive call reverseArrayRecursive(arr, start + 1, end - 1); } int main() { int arr[] = {100, 200, 300, 400, 500}; int n = sizeof(arr) / sizeof(arr[0]); reverseArrayRecursive(arr, 0, n - 1); printf("Reversed array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Explanation
- The function swaps the first and last elements and then calls itself with a smaller range.
- The base condition stops the recursion when the array is fully reversed.
Output
Reversed array: 500 400 300 200 100
Comparing the Methods
Method | Space Complexity | Time Complexity | Use Case |
---|---|---|---|
Two-Pointer Technique | O(1) | O(n) | Best for in-place operations. |
Auxiliary Array | O(n) | O(n) | Easy to implement, but uses extra space. |
Recursion | O(n) (stack) | O(n) | Elegant but memory-intensive. |
Frequently Asked Questions (FAQs)
What is the most efficient way to reverse an array in C?
The two-pointer technique is the most efficient as it works in-place with minimal memory usage.
Can arrays in C be reversed without modifying the original array?
Yes, using an auxiliary array ensures the original array remains unchanged.
What is the time complexity of reversing an array?
All three methods have a time complexity of O(n).
Can recursion be used to reverse an array?
Yes, recursion offers an elegant solution but may use more memory due to the call stack.
Why is the two-pointer technique preferred?
It is preferred for its simplicity and space efficiency.
How do I reverse an array of strings in C?
The same logic applies, but you need to handle string swapping using pointers.
Does reversing an array change its size?
No, reversing an array does not alter its size.
What happens if the array is empty?
The reversal logic handles it gracefully by avoiding any element swaps.
Is reversing an array the same as sorting it?
No, reversing rearranges elements in the opposite order, while sorting arranges them in increasing or decreasing order.
Can a multidimensional array be reversed?
Yes, but the logic must account for the array’s structure.