Reverse an Array in C++
Reverse an array in C++ two ways: std::reverse from <algorithm> and a swap loop that runs only to n / 2. Traces how the ends trade places moving inward.
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}
Sample Output
Original array: 1 2 3 4 5
The untouched copy.
Reversed (method 2): 5 4 3 2 1
After two swaps.
In this one
- How std::reverse(arr, arr + n) works with pointers as a range
- Why the swap loop stops at n / 2
- How the index n - i - 1 mirrors i
- Why reversing in place needs no extra array
How it works
- 1
Make three copies of 1 2 3 4 5.
- 2
reverse(arr1, arr1 + 5) flips arr1.
- 3
The loop swaps arr2[0] with arr2[4], then arr2[1] with arr2[3].
- 4
The middle element arr2[2] stays put.
- 5
Print all three arrays.
Swap loop for n = 5
Pairs swapped by the loop.
- 1.i = 0: swap arr2[0] and arr2[4] → 5 2 3 4 1
- 2.i = 1: swap arr2[1] and arr2[3] → 5 4 3 2 1
- 3.i = 2: loop ends because 2 < 5/2 is false
Edge cases
Looping to n instead of n / 2
Change the condition to i < n.
Every pair is swapped twice, so the array ends exactly as it started.
Even length
Use 1 2 3 4.
n / 2 = 2 swaps cover every element; there is no middle element.
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);
// 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());
// Copy back if needed
Displaying Results
The program prints:
cout << "Original array: ";
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.
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
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.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
For n = 6, how many swaps does the loop do?
Pick an answer to check it.
Question 2
What does reverse(a, a + 3) do on {1,2,3,4,5}?
Pick an answer to check it.
Questions people ask
What does arr1 + n mean?
A pointer one past the last element, which marks the end of the range for algorithms.
Is std::reverse faster?
It does the same swaps. Use it for clarity.
How do I reverse a vector?
reverse(v.begin(), v.end());