An array is a collection of items stored at contiguous memory locations. Reversal of an array is one of the most common operations to be performed on an array. In this article, you'll learn how to write your own implementation of reversal of an array using iterative and recursive approaches.

Iterative Approach to Reverse an Array

Problem Statement

You're given an array arr. You need to reverse the elements of the array, then print the reversed array. You need to implement this solution using loops.

Example 1: Let arr = [45, 12, 67, 63, 9, 23, 74]

Reversed arr = [74, 23, 9, 63, 67, 12, 45]

Thus the output is: 74 23 9 63 67 12 45.

Example 2: Let arr = [1, 2, 3, 4, 5, 6, 7, 8]

Reversed arr = [8, 7, 6, 5, 4, 3, 2, 1]

Thus the output is: 8 7 6 5 4 3 2 1.

Approach to Reverse an Array Using Loops

You can reverse the elements of an array using loops by following the approach below:

  1. Initialize index variables "i" and "j" such that they point to the first (0) and the last (sizeOfArray - 1) index of the array respectively.
  2. In a loop, swap the element at index i with the element at index j.
  3. Increment the value of i by 1 and decrement the value of j by 1.
  4. Run the loop until i < sizeOfArray/2.

C++ Program to Reverse an Array Using Loops

Below is the C++ program to reverse an array using loops:

        // C++ program to reverse the elements of an array using loops
#include <iostream>
using namespace std;


void reverseArr(int arr[], int size)
{
 for(int i=0, j=size-1; i<size/2; i++, j--)
 {
 swap(arr[i], arr[j]);
 }
}

void printArrayElements(int arr[], int size)
{
 for(int i=0; i<size; i++)
 {
 cout << arr[i] << " ";
 }
 cout << endl;
}

// Driver Code
int main()
{
 int arr[] = {45, 12, 67, 63, 9, 23, 74};
 int size = sizeof(arr)/sizeof(arr[0]);

 // Printing the original array
 cout << "Original Array: " << endl;
 printArrayElements(arr, size);

 // Reversing the array
 reverseArr(arr, size);

 // Printing the reversed array
 cout << "Reversed array: " << endl;
 printArrayElements(arr, size);

 return 0;
}

Output:

        Original Array: 
45 12 67 63 9 23 74
Reversed array:
74 23 9 63 67 12 45

Related: How to Reverse a String in C++, Python, and JavaScript

Python Program to Reverse an Array Using Loops

Below is the Python program to reverse an array using loops:

        # Python program to reverse the elements of a list using loops

def reverseList(arr, size):
    i = 0
    j = size-1
    while i<size/2:
        arr[i], arr[j] = arr[j], arr[i]
        i = i + 1
        j = j - 1

def printListElements(arr, size):
    for i in range(size):
        print(arr[i], end=" ")
    print()

# Driver Code
arr = [45, 12, 67, 63, 9, 23, 74]
size = len(arr)

# Printing the original array
print("Original Array:")
printListElements(arr, size)

# Reversing the array
reverseList(arr, size)

# Printing the reversed array
print("Reversed Array:")
printListElements(arr, size)

Output:

        Original Array: 
45 12 67 63 9 23 74
Reversed array:
74 23 9 63 67 12 45

JavaScript Program to Reverse an Array Using Loops

Below is the JavaScript program to reverse an array using loops:

Related: An Introduction to the Merge Sort Algorithm

        // JavaScript program to reverse the elements of an array using loops
function reverseArr(arr, size) {
 for(let i=0, j=size-1; i<(size)/2; i++, j--) {
 [arr[i], arr[j]] = [arr[j], arr[i]];
 }
}

function printArrayElements(arr, size) {
 for(let i=0; i<size; i++) {
 document.write(arr[i] + " ");
 }
 document.write("
");
}

// Driver Code
var arr = [45, 12, 67, 63, 9, 23, 74];
var size = arr.length;

// Printing the original array
document.write("Original Array: " + "
");
printArrayElements(arr, size);

// Reversing the array
reverseArr(arr, size);

// Printing the reversed array
document.write("Reversed Array: " + "
");
printArrayElements(arr, size);

Output:

        Original Array: 
45 12 67 63 9 23 74
Reversed array:
74 23 9 63 67 12 45

Recursive Approach to Reverse an Array

Problem Statement

You're given an array arr. You need to reverse the elements of the array, then print the reversed array. You need to implement this solution using recursion.

Example 1: Let arr = [45, 12, 67, 63, 9, 23, 74]

Reversed arr = [74, 23, 9, 63, 67, 12, 45]

Thus the output is 74 23 9 63 67 12 45.

Example 2: Let arr = [1, 2, 3, 4, 5, 6, 7, 8]

Reversed arr = [8, 7, 6, 5, 4, 3, 2, 1]

Thus the output is 8 7 6 5 4 3 2 1.

Approach to Reverse an Array Using Recursion

You can reverse the elements of an array using recursion by following the approach below:

  1. Initialize index variables start and end such that they point to the first (0) and the last (sizeOfArray - 1) index of the array respectively.
  2. Swap the element at the index start with the element at the index end.
  3. Recursively call the reverse function. In parameters of the reverse function, increment the value of start by 1 and decrement the value of end by 1.
  4. Stop the recursion when the value of the start variable is greater than or equal to the value of the end variable.

C++ Program to Reverse an Array Using Recursion

Below is the C++ program to reverse an array using recursion:

        // C++ program to reverse an array using recursion
#include <iostream>
using namespace std;

void reverseArr(int arr[], int start, int end)
{
 if (start >= end)
 {
 return;
 }

swap(arr[start], arr[end]);
 reverseArr(arr, start+1, end-1);
}

void printArrayElements(int arr[], int size)
{
 for(int i=0; i<size; i++)
 {
 cout << arr[i] << " ";
 }
 cout << endl;
}

// Driver Code
int main()
{
 int arr[] = {45, 12, 67, 63, 9, 23, 74};
 int size = sizeof(arr)/sizeof(arr[0]);

 // Printing the original array
 cout << "Original Array: " << endl;
 printArrayElements(arr, size);

 // Reversing the array
 reverseArr(arr, 0, size-1);

 // Printing the reversed array
 cout << "Reversed array: " << endl;
 printArrayElements(arr, size);

 return 0;
}

Output:

        Original Array: 
45 12 67 63 9 23 74
Reversed array:
74 23 9 63 67 12 45

Python Program to Reverse an Array Using Recursion

Below is the Python program to reverse an array using recursion:

Related: Dynamic Programming: Examples, Common Problems, and Solutions

        # Python program to reverse an array using recursion

def reverseList(arr, start, end):
    if start >= end:
        return

    arr[start], arr[end] = arr[end], arr[start]
    reverseList(arr, start+1, end-1)

def printListElements(arr, size):
    for i in range(size):
        print(arr[i], end=" ")
    print()

# Driver Code
arr = [45, 12, 67, 63, 9, 23, 74]
size = len(arr)

# Printing the original array
print("Original Array:")
printListElements(arr, size)

# Reversing the array
reverseList(arr, 0, size-1)

# Printing the reversed array
print("Reversed Array:")
printListElements(arr, size)

Output:

        Original Array: 
45 12 67 63 9 23 74
Reversed array:
74 23 9 63 67 12 45

JavaScript Program to Reverse an Array Using Recursion

Below is the JavaScript program to reverse an array using recursion:

Related: How to Find the Sum of Natural Numbers Using Recursion

        // JavaScript program to reverse an array using recursion

function reverseArr(arr, start, end)
{
 if (start >= end)
 {
 return;
 }
 [arr[start], arr[end]] = [arr[end], arr[start]];
 reverseArr(arr, start+1, end-1);
}

function printArrayElements(arr, size)
{
 for(let i=0; i<size; i++)
 {
 document.write(arr[i] + " ");
 }
 document.write("
");
}

// Driver Code
var arr = [45, 12, 67, 63, 9, 23, 74];
let size = arr.length;

// Printing the original array
document.write("Original Array: " + "
");
printArrayElements(arr, size);

// Reversing the array
reverseArr(arr, 0, size-1);

// Printing the reversed array
document.write("Reversed Array: " + "
");
printArrayElements(arr, size);

Output:

        Original Array: 
45 12 67 63 9 23 74
Reversed array:
74 23 9 63 67 12 45

Use Recursion to Solve Problems

A recursive function is a function that calls itself. In recursion, a problem is solved by breaking down the problems into smaller, simpler versions of themselves.

There are many advantages of recursion: the recursive code is shorter than an iterative code, it can be used to solve the problems that are naturally recursive, it can be used in infix, prefix, postfix evaluations, recursion reduces the time needed to write and debug code, etc.

Recursion is a favorite topic of interviewers in technical interviews. You must know about recursion and how to use it while writing code to be the most efficient programmer you can be.