Last Updated on May 11, 2023 by Prepbytes
When an array is reversed, the items of the original array are changed in order. With this method, the first element becomes the last and the last element becomes the first one in the array. However, the procedure keeps going until all characters or array elements are entirely reversed.
For instance, if an array has elements like "C," "O," "D," and "E," and we reverse all of those elements, the result is "E," "D," "O," and "C." In this manner, the array’s characters are all reversed.
Different Ways to Reverse an Array
The different approaches to obtaining the reverse array in C++ are listed below.
- Using Recursive Approach to reverse an array
- Using the Iterative Approach to reverse an array
- Using the reverse() Function to reverse an array
- Using the Concept of Swapping to reverse an array
- Using a User-defined Function to reverse an array
Approach 1- Recursive Approach
This method involves inverting the remainder of the array while repeatedly splitting it into smaller subarrays.
To reverse the array arr from index start to index end, let’s define the recursive function reverse(start, end, arr). The start and end are both set to 0 at the beginning. We switch the arr[end] and arr[start] in each iteration of the recursive step before calling the function for the subarray start+1 and end-1.
The mechanism described above is emulated as follows::
Algorithm
- In the case when start=0 and end=N-1, call the function reverse(start, end, arr).
- Exchange the letters arr[start] and arr[end].
-
The same code is called recursively for the subarrays start+1 and end-1.
reverse(start,end,arr): swap(arr[start],start[end]) reverse(start+1,end-1,arr)
Now, let’s look into the code implementation to reverse elements of an array in C++.
Code Implementation in C++
#include<bits/stdc++.h> using namespace std; //Reverse array function void reverse(int start, int end, int arr[]){ // Base case if(start >= end) return; //Swap the arr[start] and arr[end] swap(arr[start],arr[end]); //Recursive step. reverse(start+1,end-1,arr); } //Auxuliary function to print the array void printArray(int n, int arr[]){ for(int i=0;i<n;i++) cout << arr[i] << " "; } //Driver program int main(){ //Input array int arr[] = {1,2,3,4,5}; //Size of the input array int n = sizeof arr/sizeof arr[0]; //call the function for the reverse reverse(0,n-1,arr); //Print the array printArray(n,arr); }
Output
5 4 3 2 1
Time Complexity
The time complexity is O(n) since we only call the reverse function n/2 times, where n is the length of the array.
Space Complexity
Since there is no auxiliary space used in the implementation, the space complexity is O(1).
Approach 2 – Iterative Approach
In this method, we iterate the array up to n/2 and swap each element at position i with the element at index (n-i-1) of the array. The method uses two pointers, the first of which is i and the second of which is (n-i-1).
Below is a simulation of the process:
Algorithm
- Set the two variables start and end to 0 and n, respectively.
- Swap the elements of the array and change the pointers such that start = start + 1 and end = end-1 are used when iterating over the array in a loop.
Code Implementation in C++
#include<bits/stdc++.h> using namespace std; // Function to reverse the array void reverse(int n, int arr[]){ //start and end pointers for(int i=0;i<n/2;i++) swap(arr[i],arr[n-i-1]); } //Function to print the array void printArray(int n, int arr[]){ for(int i=0;i<n;i++) cout << arr[i] << " "; } //Driver function int main(){ //Input array int arr[] = {1,2,3,4,5}; //size of the array int n = sizeof arr / sizeof arr[0]; //reverse the array reverse(n,arr); //Print the array printArray(n,arr); }
Output:
5 4 3 2 1
Time Complexity
The time complexity is O(n) because we just iterate over the array once.
Space Complexity
The space complexity is O(1) because we don’t need any auxiliary space during implementation.
Approach 3 – Reverse an Array in C++ Using the reverse() Function
Arrays can be reversed using the built-in reverse function in C++.
Algorithm
Use the reverse function and supply the starting and ending addresses of the array as arguments.
Syntax:
reverse(arr,arr+n) will reverse the array arr of n elements.
Code Implementation in C++
#include<bits/stdc++.h> using namespace std; //reverse the array using in-built function void reverse(int arr[], int n){ reverse(arr,arr+n); } //function to print the array void printArray(int arr[], int n){ for(int i=0;i<n;i++) cout << arr[i] << " "; } //Driver program int main(){ //Input array int arr[] = {1,2,3,4,5}; //Size of the array int n = sizeof arr/sizeof arr[0]; // reverse the array reverse(arr,n); //Print the given array printArray(arr,n); }
Output:
5 4 3 2 1
Time Complexity
Since the reverse function has an O(n) time complexity, the above implementation also has an O(n) time complexity.
Space Complexity
The space complexity is O(1) because there is no auxiliary space used in the implementation.
Approach 4- Using the Concept of Swapping
In this method, we invert the array utilizing a third variable by applying the concept of swapping or interchanging the values. Using a temporary variable called temp, we iterate over the array from i=0 to i=n/2 and swap the values at arr[i] and arr[n-i-1].
Algorithm
- Set the value of start = 0 and end = n-1;
- Run a while loop and perform the following steps:
- Interchange the values arr[start] and arr[end].
- Increment start by 1 and decrement end by 1.
- If start reaches the value upto n/2 or start >= end then terminate, else repeat step a and b.
Code Implementation in C++
#include<bits/stdc++.h> using namespace std; //Function to reverse the array void reverse(int n, int arr[]){ int start = 0 , end = n-1; while(start < end){ int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start += 1; end -= 1; } } //Print array void printArray(int n, int arr[]){ for(int i=0;i<n;i++) cout << arr[i] << " "; } //Driver program int main(){ //Input array int arr[] = {1,2,3,4,5}; //Size of the array int n = sizeof arr / sizeof arr[0]; //call the reverse function reverse(n,arr); //Print the array printArray(n,arr); }
Output:
5 4 3 2 1
Time Complexity
Since the operations are proportional to n and we are iterating through the array up to n/2, the time complexity is O(n).
Space Complexity
The space complexity is O(1) because the implementation uses no additional storage.
Approach 5 – User-defined Function
The logic is the same in this method as it was in the iterative method, but we reverse the array using user-defined procedures rather than any built-in library functions.
Algorithm
- Define a function reverseArray which is used to reverse the array.
- In the function initialize two pointers start=0 and end=n-1.
- Swap the elements at start and end and move the pointers start as start+1 and end as end-1.
Code Implementation in C++
#include<bits/stdc++.h> using namespace std; //Function to reverse the array void reverseArray(int arr[], int n); void printArray(int arr[], int n){ for(int i=0; i<n; i++) cout << arr[i] << " "; cout << endl; } //Driver program int main(){ //Input array int arr[] = {1,2,3,4,5}; //Length of the array int n = sizeof arr/sizeof arr[0]; //Function to reverse the array reverseArray(arr,n); //Function to print the array printArray(arr,n); } //Function to reverse the array void reverseArray(int ar[], int n){ int i, j, temp; j = n - 1; for ( i = 0; i < j; i++, j--) { temp = ar[i]; ar[i] = ar[j]; ar[j] = temp; } }
Output
5 4 3 2 1
Time Complexity
The array is iterated over only once, hence the time complexity is O(n).
Space Complexity
Since no auxiliary space is used in the implementation, the space complexity is reduced to O(1).
Approach 6 – Reverse an Array in C++ Using the Pointers
In this method, we reverse the array using the C++ pointer notion. Specifically, we utilize two pointers: pointer1 points to the array’s beginning location, and pointer2 points to its ending address.
The elements at these two positions are switched, and the pointer1 and pointer2 pointers are then transferred to the addresses of the next and previous array elements, respectively.
Algorithm
- Set up two pointers pointer1 and pointer2 with the array’s start and end addresses.
- Change the values at the start and finish locations, add one to the start pointer, and subtract one from the end pointer.
- Until the start address passes the finish address or they are equal, repeat step two.
Code Implementation in C++
#include<bits/stdc++.h> using namespace std; //swap function void swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } //reverse array function void reverse(int arr[], int n){ //Pointer pointing at the beginning int *pointer1 = arr; //Pointer pointing at the end of the array int *pointer2 = arr + n -1; while(pointer1 < pointer2){ swap(pointer1,pointer2); pointer1++; pointer2--; } } ///Print the array void print(int *arr, int n){ //size at the end of the array int *size = arr + n; //Position pointing to the beginning of the // array int *pos = arr; for(pos = arr;pos < size; pos++) cout << *pos << " "; } //Driver program int main(){ //Input array int arr[] = {1,2,3,4,5}; //Length of the array int n = sizeof arr / sizeof arr[0]; //reverse the array reverse(arr,n); //print the array print(arr,n); }
Output
5 4 3 2 1
Time Complexity
The time complexity is O(n) since we iterate over the array exactly once while switching elements.
Space Complexity
The space complexity is O(1) because there is no extra space being used.
Conclusion
We discussed 6 different approaches to reverse elements of an array in C++. Besides this, there are more approaches to reverse elements of an array in C++, you just need to practice and build your logical skills for that. Reversing an element in an array is an amazing question for the beginner level.
FAQ Related to Reverse Elements of an Array in C++
Q1. Is there any function to reverse an array in C++?
Ans. Yes, in C++, there is an in-built reverse function that is used to reverse arrays.
Q2. Is reversing an array an important question in C++?
Ans. The answer is YES, reverse elements of an array in C++ are important as it is asked in many companies i.e. TCS, Wipro, Accenture, Capgemini.
Q3. What is array sorting in C++?
Ans. Sorting in C++ is a concept in which the elements of an array are rearranged in a logical order. This order can be from lowest to highest or highest to lowest. Sorting an unsorted array helps to solve many problems such as searching for the minimum or maximum element, etc.