Reverse an Array in C++
Reverse an Array in C++ (7 Programs With Output).
1#include <iostream>2#include <algorithm>3using namespace std;45int main() {6 int arr[] = {1, 2, 3, 4, 5};7 int n = 5;89 // Method 1: Using reverse() from algorithm10 int arr1[] = {1, 2, 3, 4, 5};11 reverse(arr1, arr1 + n);1213 // Method 2: Using swap14 int arr2[] = {1, 2, 3, 4, 5};15 for (int i = 0; i < n / 2; i++) {16 swap(arr2[i], arr2[n - i - 1]);17 }1819 cout << "Original array: ";20 for (int i = 0; i < n; i++) {21 cout << arr[i] << " ";22 }23 cout << endl;2425 cout << "Reversed (method 1): ";26 for (int i = 0; i < n; i++) {27 cout << arr1[i] << " ";28 }29 cout << endl;3031 cout << "Reversed (method 2): ";32 for (int i = 0; i < n; i++) {33 cout << arr2[i] << " ";34 }35 cout << endl;3637 return 0;38}
Output
Original array: 1 2 3 4 5 Reversed (method 1): 5 4 3 2 1 Reversed (method 2): 5 4 3 2 1
What's going on
The program reverses the order of elements in an array. For example:
The reversal swaps elements from both ends, moving toward the center until all elements are swapped.
Example:
Header Files Used
Declaring Variables
The program declares:
int arr[] = {1, 2, 3, 4, 5};
int n = 5;
Method 1: Using reverse() Algorithm (STL)
int arr1[] = {1, 2, 3, 4, 5};
reverse(arr1, arr1 + n);
This is the simplest and most recommended method:
How it works:
Advantages:
Example:
int arr[] = {1, 2, 3, 4, 5};
reverse(arr, arr + 5);
1// arr is now [5, 4, 3, 2, 1]
Method 2: Using swap() in a Loop
int arr2[] = {1, 2, 3, 4, 5};
for (int i = 0; i < n / 2; i++) {
swap(arr2[i], arr2[n - i - 1]);
}
This method manually swaps elements:
How it works:
Why n/2?
Advantages:
Other Methods (Mentioned but not shown in code)
Method 3: Using Two Pointers
int left = 0, right = n - 1;
while (left < right) {
swap(arr[left], arr[right]);
left++;
right--;
}
Method 4: Using Recursion
void reverseRecursive(int arr[], int start, int end) {
if (start >= end) return;
swap(arr[start], arr[end]);
reverseRecursive(arr, start + 1, end - 1);
}
Method 5: Using Stack
stack<int> s;
for (int i = 0; i < n; i++) s.push(arr[i]);
for (int i = 0; i < n; i++) {
arr[i] = s.top();
s.pop();
}
Method 6: Using Temporary Array
int temp[n];
for (int i = 0; i < n; i++) {
temp[i] = arr[n - i - 1];
}
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
}
Method 7: Using Vector
vector<int> vec(arr, arr + n);
reverse(vec.begin(), vec.end());
1// Copy back if needed
Displaying Results
The program prints:
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << "Reversed (method 1): ";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
Output:
Original array: 1 2 3 4 5
Reversed (method 1): 5 4 3 2 1
Reversed (method 2): 5 4 3 2 1
Both methods produce the same reversed array.
1cout << "Original array: ";
Understanding the Reversal Process
Visual Example
(for [1, 2, 3, 4, 5]):
Initial: [1, 2, 3, 4, 5]
↑ ↑
start end
Step 1: [5, 2, 3, 4, 1] (swap 1 and 5)
↑ ↑
start end
Step 2: [5, 4, 3, 2, 1] (swap 2 and 4)
↑ ↑
start end
Done: [5, 4, 3, 2, 1] (middle element 3 stays)
Key Insight
: Only need to swap n/2 pairs. Middle element (if odd length) doesn't move.
When to Use Each Method
-
reverse() Algorithm
: Best for most cases - simple, optimized, recommended.
-
swap() in Loop
: Good for learning - shows the algorithm clearly.
-
Two Pointers
: Similar to swap method - clear and efficient.
-
Recursion
: Educational - helps understand recursive thinking.
-
Stack
: Demonstrates stack usage - educational purpose.
-
Temporary Array
: When you need to preserve original - uses extra space.
-
Vector
: When working with vectors or need dynamic size.
Best Practice
: Use reverse() for most applications - it's simple, efficient, and works with any container.
Time and Space Complexity
Time Complexity
:
Space Complexity
:
Common Use Cases
Algorithm Problems
:
Data Processing
:
Interview Questions
:
Important Considerations
Array Bounds
:
Odd vs Even Length
:
In-place vs Copy
:
return 0;
This ends the program successfully.