Table of contents
1.
Introduction
2.
Reverse an Array Using a Loop
2.1.
Implementation
2.2.
C++
2.3.
Java
2.4.
Python
2.5.
Time Complexity
2.6.
Space Complexity
3.
Reverse an Array Using Inbuilt Methods
3.1.
Implementation
3.2.
C++
3.3.
Java
3.4.
Python
3.5.
Time Complexity
3.6.
Space Complexity
4.
Reverse an Array Using Recursion
4.1.
Implementation
4.2.
C++
4.3.
Java
4.4.
Python
4.5.
Time Complexity
4.6.
Space Complexity
5.
Reverse an Array Using Stack
5.1.
Implementation
5.2.
C++
5.3.
Java
5.4.
Python
5.5.
Time Complexity
5.6.
Space Complexity
6.
Frequently Asked Questions
6.1.
How to reverse order in array in C?
6.2.
How do you reverse an array in C++ using function?
6.3.
How to reverse a 2D array in C?
6.4.
Can I reverse an array in less than O(N) time complexity?
7.
Conclusion
Last Updated: Aug 2, 2024
Medium

Reverse an Array (C++/Java/Python)

Author Komal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

An array is the most used data structure. An array is a collection of elements of similar data types. Suppose there are 50 students in your class, and you want to store the name of every student in your program. One option is that you make 50 variables and store their names in these or the more optimal way is that you make an array of strings and store names of all in this array.

reversing an array

Elements in arrays are stored in contiguous memory locations and the most important thing you should know about an array is that the name of an array is the pointer to the first element of the array. In this blog, we’ll learn how we can reverse an array.

Suppose you are given an array like this:

1 2 3 4 5 6

Now you are given the task of reversing this array i.e.

6 5 4 3 2 1

We can reverse the arrays using various methods like the iterative method or the recursive method. Let’s see all these methods here:

Reverse an Array Using a Loop

Reversing an array using a loop involves iterating through the array from both ends and swapping elements until the entire array is reversed.

Implementation

  • C++
  • Java
  • Python

C++

#include <iostream>
using namespace std;

void reverseArray(int arr[], int n) {
int start = 0, end = n - 1;
while (start < end) {
swap(arr[start], arr[end]);
start++;
end--;
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java

class Main {
static void reverseArray(int arr[]) {
int start = 0, end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
reverseArray(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
You can also try this code with Online Java Compiler
Run Code

Python

def reverse_array(arr):
start, end = 0, len(arr) - 1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1

arr = [1, 2, 3, 4, 5]
reverse_array(arr)
print(arr)
You can also try this code with Online Python Compiler
Run Code

Output

5 4 3 2 1

Time Complexity

The time complexity for reversing an array using a loop is O(n), where n is the number of elements in the array.

Space Complexity

The space complexity is O(1) as it requires only a constant amount of extra space for variables.

Reverse an Array Using Inbuilt Methods

Reversing an array using inbuilt methods involves using predefined functions or methods provided by the programming language to reverse the elements of the array.

Implementation

  • C++
  • Java
  • Python

C++

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
reverse(arr, arr + n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java

import java.util.Arrays;

class Main {
public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
Arrays.sort(arr);
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}
You can also try this code with Online Java Compiler
Run Code

Python

arr = [1, 2, 3, 4, 5]
arr.reverse()
print(arr)
You can also try this code with Online Python Compiler
Run Code

Output

5 4 3 2 1

Time Complexity

The time complexity for reversing an array using inbuilt methods varies depending on the implementation, but it is typically O(n).

Space Complexity

The space complexity depends on the implementation, but it is usually O(1) or O(n) if additional space is required for the method.

Reverse an Array Using Recursion

Reversing an array using recursion involves defining a recursive function to reverse a subarray and then swapping the elements recursively.

Implementation

  • C++
  • Java
  • Python

C++

#include <iostream>
using namespace std;

void reverseArray(int arr[], int start, int end) {
if (start >= end)
return;
swap(arr[start], arr[end]);
reverseArray(arr, start + 1, end - 1);
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java

class Main {
static void reverseArray(int arr[], int start, int end) {
if (start >= end)
return;
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
reverseArray(arr, start + 1, end - 1);
}

public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
reverseArray(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
You can also try this code with Online Java Compiler
Run Code

Python

def reverse_array(arr, start, end):
if start >= end:
return
arr[start], arr[end] = arr[end], arr[start]
reverse_array(arr, start + 1, end - 1)

arr = [1, 2, 3, 4, 5]
reverse_array(arr, 0, len(arr) - 1)
print(arr)
You can also try this code with Online Python Compiler
Run Code

Output

5 4 3 2 1

Time Complexity

The time complexity for reversing an array using recursion is O(n), where n is the number of elements in the array.

Space Complexity

The space complexity for the recursive implementation is O(n) due to the recursive calls made on the call stack.

Reverse an Array Using Stack

Reversing an array using a stack involves pushing the elements of the array onto a stack and then popping them off the stack to reverse the order.

Implementation

  • C++
  • Java
  • Python

C++

#include <iostream>
#include <stack>
using namespace std;

void reverseArray(int arr[], int n) {
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();
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
reverseArray(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java

import java.util.Stack;

class Main {
static void reverseArray(int arr[]) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < arr.length; i++) {
stack.push(arr[i]);
}
for (int i = 0; i < arr.length; i++) {
arr[i] = stack.pop();
}
}

public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5};
reverseArray(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
You can also try this code with Online Java Compiler
Run Code

Python

def reverse_array(arr):
stack = []
for num in arr:
stack.append(num)
for i in range(len(arr)):
arr[i] = stack.pop()

arr = [1, 2, 3, 4, 5]
reverse_array(arr)
print(arr)
You can also try this code with Online Python Compiler
Run Code

Output

5 4 3 2 1

Time Complexity

The time complexity for reversing an array using a stack is O(n), where n is the number of elements in the array.

Space Complexity

The space complexity is O(n) as it requires extra space for the stack to store the elements.

Frequently Asked Questions

How to reverse order in array in C?

To reverse the order of elements in an array in C, iterate through the array from both ends and swap elements until reaching the middle of the array.

How do you reverse an array in C++ using function?

In C++, you can reverse an array using the reverse() function from the <algorithm> header. Pass the beginning and end iterators of the array to the function to reverse its elements.

How to reverse a 2D array in C?

To reverse a 2D array in C, iterate through each row of the array and reverse the order of elements within each row using the same technique as reversing a 1D array.

Can I reverse an array in less than O(N) time complexity?

No, you can’t do that because for reversing an array, you need to travel all the array elements in one traversal time complexity will be O(N).

Conclusion

In this article, you learned how to reverse an array using various approaches like recursion, stack, loop, etc. An array is the most asked data structure in interviews, so it’s mandatory to learn about different operations you can perform on an array.

We also discussed the time and space complexity of reversing an array. You should always try to find the space and time complexity of any program you write, as in the real world, we have to calculate in advance the time and space our program will take.

Recommended problems - 

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more! 

Give your career an edge over others by considering our premium courses!

Live masterclass