Summary: In this tutorial, we will learn different ways to reverse an array in C++ programming language.
Example:
Array : {5, 6, 2, 3} Reverse : {3, 2, 6, 5}
There are multiple ways to reverse an array in C++. Let’s discuss each of them.
Method 1: Using an Extra Array
The idea is to create a new array and assign elements to it from the input array in reversed order.
To do this, we simply have to:
- Initialize an array with values.
- Declare a new empty array of the same size.
- Loop from the back of the input array and assign its elements to the new array.
- Output the reverse array.
Here is implementation of it in C++:
#include <iostream>
using namespace std;
int main()
{
int array[] = {5, 6, 2, 3};
//find the size of array
int size = sizeof(array)/sizeof(array[0]);
//declare new array to store reverse of original array
int k=0, reverse[size];
//Loop from back and assig value to new array
for(int i=size-1; i>=0; ){
reverse[k++] = array[i--];
}
//output the reverse array
for(int i=0; i<size; i++){
cout << reverse[i] <<", ";
}
return 0;
}
Output:
3, 2, 6, 5Method 2: Without using an Extra Array
The logic to reverse an array without using another array in C++ is to swap 1st element with the last element, then the 2nd element with the 2nd last element, then 3rd, 4th… until we reach the middle element.
#include <iostream>
using namespace std;
//Function to display array
void display(int array[], int size){
for(int i=0; i<size; i++){
cout << array[i] << " ";
}
cout << "\n";
}
int main()
{
int array[] = {4, 5, 1, 3, 9};
int size= sizeof(array)/sizeof(array[0]), temp=0;
int i=0; //pointing 1st element of the array
int j=size- 1; //pointing last element of the array
while( i < j){
//swap
temp = array[i];
array[i] = array[j];
array[j] = temp;
//Update i and j
i++;
j--;
}
//output the array
display(array, size);
return 0;
}
Output:
9 3 1 5 4We used ‘i’ and ‘j’ to store the index of elements that needed to be swapped.
Inside while loop, we increment ‘i’ (i++
) and decrement ‘j’ (j--
), until j exceeds i.
In this method, we have not used any auxiliary array to fulfill the purpose, hence this method is an in-place algorithm.
Method 3: using Recursion
Using recursion, we can easily reverse an array in in-place. The idea is to extract the element on each recursive call and reassign it to the array during the traceback process but in the reverse order.
For example, the first element will be assigned at the last of the array, like the following:
//extract int element = array[0] //reassign int array[size-1] = element
Here is the full implementation of the idea in C++:
#include<iostream>
using namespace std;
//array
int array[] = {5, 6, 2, 3};
//size of the array
int size = sizeof(array)/sizeof(array[0]);
void reverse(int array[], int n){
//base condition
if(n==size)
return;
//extract element
int element = array[n];
//recursive call for next element
reverse(array, n+1);
//assign during the traceback
array[size-n-1] = element;
}
int main()
{
//call the recusrive funtion (starting the first element)
reverse(array, 0);
//output the array
for(int i=0; i<size; i++){
cout << array[i] <<" ";
}
return 0;
}
Output:
3 2 6 5Although we are not using any extra array but still the method is not efficient in terms of space complexity.
For a large array, the recursion stack and the variable element
inside it (holding an array element) can cause space inefficiency.
Comment below, if you know any other method to reverse an array in C++.