Posted on by Kalkicode
Code Array

# Reverse the array elements

The problem at hand is to reverse the elements of an array. Reversing an array means changing the order of its elements such that the first element becomes the last, the second becomes the second-to-last, and so on.

## Problem Statement

Given an array, we want to reverse its elements. For instance, if we have the array `[1, 2, 3, 4, 5, 6, 7, 8]`, after reversing, it should become `[8, 7, 6, 5, 4, 3, 2, 1]`.

## Example

Let's consider an example to illustrate the problem. We have an array: `[4, 12, 7, -2, 8]`. After reversing, the array should become: `[8, -2, 7, 12, 4]`.

## Idea to Solve

To reverse an array, we need to swap elements from the two ends of the array and gradually move towards the center. The swapping continues until we reach the midpoint of the array.

## Pseudocode

``````reverse_element(arr, size):
front = 0
tail = size - 1
while front < tail:
swap(arr[front], arr[tail])
front++
tail--

main():
arr = [1, 2, 3, 4, 5, 6, 7, 8]
reverse_element(arr)
display(arr)``````

## Algorithm Explanation

1. Initialize two pointers: `front` pointing to the beginning of the array and `tail` pointing to the end of the array.
2. Enter a loop that continues as long as `front` is less than `tail`.
3. Inside the loop, swap the elements at positions `front` and `tail`.
4. Increment `front` and decrement `tail`.
5. Continue the loop until `front` becomes greater than or equal to `tail`. This means we've swapped all elements and effectively reversed the array.

## Code Solution

``````// C Program to
// Reverse the array elements
#include <stdio.h>
//Function which is swapping two array elements of given location
void swap_element(int arr[], int i, int j)
{
//Get i location element
int temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", arr[i]);
}
printf("\n");
}
//Function which is Reversing given array elements
void reverse_element(int arr[], int size)
{
int front = 0;
int tail = size - 1;
//Just before the Reversing array elements
printf("Before Reversing elements \n");
display(arr, size);
while (front < tail)
{
//Swap the array elements
swap_element(arr, front, tail);
front++;
tail--;
}
//After modify array elements
printf("After Reversing elements \n");
display(arr, size);
printf("\n");
}
int main()
{
//Define array elements
int arr1[] = {
1,
2,
3,
4,
5,
6,
7,
8
};
int size = sizeof(arr1) / sizeof(arr1[0]);
reverse_element(arr1, size);
//Define array elements
int arr2[] = {
7,
2,
9,
-7,
2,
1,
8,
2,
2
};
size = sizeof(arr2) / sizeof(arr2[0]);
reverse_element(arr2, size);
return 0;
}``````

#### Output

``````Before Reversing elements
1 2 3 4 5 6 7 8
After Reversing elements
8 7 6 5 4 3 2 1

Before Reversing elements
7 2 9 -7 2 1 8 2 2
After Reversing elements
2 2 8 1 2 -7 9 2 7
``````
``````/*
Java Program
Reverse the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
public void swap_element(int[] arr, int i, int j)
{
//Get i location element
int temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i] + " ");
}
System.out.print("\n");
}
//Function which is Reversing given array elements
public void reverse_element(int[] arr, int size)
{
int front = 0;
int tail = size - 1;
//Just before the Reversing array elements
System.out.print("Before Reversing elements \n");
display(arr, size);
while (front < tail)
{
//Swap the array elements
swap_element(arr, front, tail);
front++;
tail--;
}
//After modify array elements
System.out.print("After Reversing elements \n");
display(arr, size);
System.out.print("\n");
}
public static void main(String[] args)
{
MyArray obj = new MyArray();
//Define array elements
int[] arr1 = {
1,
2,
3,
4,
5,
6,
7,
8
};
int size = arr1.length;
obj.reverse_element(arr1, size);
//Define array elements
int[] arr2 = {
7,
2,
9,
-7,
2,
1,
8,
2,
2
};
size = arr2.length;
obj.reverse_element(arr2, size);
}
}``````

#### Output

``````Before Reversing elements
1  2  3  4  5  6  7  8
After Reversing elements
8  7  6  5  4  3  2  1

Before Reversing elements
7  2  9  -7  2  1  8  2  2
After Reversing elements
2  2  8  1  2  -7  9  2  7
``````
``````/*
C++ Program
Reverse the array elements
*/
#include<iostream>
using namespace std;
class MyArray
{
public:
//Function which is swapping two array elements of given location
void swap_element(int arr[], int i, int j)
{
//Get i location element
int temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i] << " ";
}
cout << "\n";
}
//Function which is Reversing given array elements
void reverse_element(int arr[], int size)
{
int front = 0;
int tail = size - 1;
cout << "Before Reversing elements \n";
this->display(arr, size);
while (front < tail)
{
//Swap the array elements
this->swap_element(arr, front, tail);
front++;
tail--;
}
cout << "After Reversing elements \n";
this->display(arr, size);
cout << "\n";
}
};
int main()
{
MyArray obj ;
int arr1[] = {
1,
2,
3,
4,
5,
6,
7,
8
};
int size = sizeof(arr1) / sizeof(arr1[0]);
obj.reverse_element(arr1, size);
int arr2[] = {
7,
2,
9,
-7,
2,
1,
8,
2,
2
};
size = sizeof(arr2) / sizeof(arr2[0]);
obj.reverse_element(arr2, size);
return 0;
}``````

#### Output

``````Before Reversing elements
1  2  3  4  5  6  7  8
After Reversing elements
8  7  6  5  4  3  2  1

Before Reversing elements
7  2  9  -7  2  1  8  2  2
After Reversing elements
2  2  8  1  2  -7  9  2  7
``````
``````/*
C# Program
Reverse the array elements
*/
using System;
class MyArray
{
//Function which is swapping two array elements of given location
public void swap_element(int[] arr, int i, int j)
{
//Get i location element
int temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; i++)
{
Console.Write(" " + arr[i] + " ");
}
Console.Write("\n");
}
//Function which is Reversing given array elements
public void reverse_element(int[] arr, int size)
{
int front = 0;
int tail = size - 1;
Console.Write("Before Reversing elements \n");
display(arr, size);
while (front < tail)
{
//Swap the array elements
swap_element(arr, front, tail);
front++;
tail--;
}
Console.Write("After Reversing elements \n");
display(arr, size);
Console.Write("\n");
}
public static void Main(String[] args)
{
MyArray obj = new MyArray();
int[] arr1 = {
1,
2,
3,
4,
5,
6,
7,
8
};
int size = arr1.Length;
obj.reverse_element(arr1, size);
int[] arr2 = {
7,
2,
9,
-7,
2,
1,
8,
2,
2
};
size = arr2.Length;
obj.reverse_element(arr2, size);
}
}``````

#### Output

``````Before Reversing elements
1  2  3  4  5  6  7  8
After Reversing elements
8  7  6  5  4  3  2  1

Before Reversing elements
7  2  9  -7  2  1  8  2  2
After Reversing elements
2  2  8  1  2  -7  9  2  7
``````
``````<?php
/*
Php Program
Reverse the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
function swap_element( \$arr, \$i, \$j)
{
//Get i location element
\$temp = \$arr[\$i];
//set new values
\$arr[\$i] = \$arr[\$j];
\$arr[\$j] = \$temp;
}
//Function which is display array elements
function display( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo " ". \$arr[\$i] ." ";
}
echo "\n";
}
//Function which is Reversing given array elements
function reverse_element( & \$arr, \$size)
{
\$front = 0;
\$tail = \$size - 1;
echo "Before Reversing elements \n";
\$this->display(\$arr, \$size);
while (\$front < \$tail)
{
//Swap the array elements
\$this->swap_element(\$arr, \$front, \$tail);
\$front++;
\$tail--;
}
echo "After Reversing elements \n";
\$this->display(\$arr, \$size);
echo "\n";
}
}

function main()
{
\$obj = new MyArray();
//Define array elements
\$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8);
\$size = count(\$arr1);
\$obj->reverse_element(\$arr1, \$size);
//Define array elements
\$arr2 = array(7, 2, 9, -7, 2, 1, 8, 2, 2);
\$size = count(\$arr2);
\$obj->reverse_element(\$arr2, \$size);
}
main();``````

#### Output

``````Before Reversing elements
1  2  3  4  5  6  7  8
After Reversing elements
1  2  3  4  5  6  7  8

Before Reversing elements
7  2  9  -7  2  1  8  2  2
After Reversing elements
7  2  9  -7  2  1  8  2  2
``````
``````/*
Node Js Program
Reverse the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
swap_element(arr, i, j)
{
//Get i location element
var temp = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i] + " ");
}
process.stdout.write("\n");
}
//Function which is Reversing given array elements
reverse_element(arr, size)
{
var front = 0;
var tail = size - 1;
process.stdout.write("Before Reversing elements \n");
this.display(arr, size);
while (front < tail)
{
//Swap the array elements
this.swap_element(arr, front, tail);
front++;
tail--;
}
process.stdout.write("After Reversing elements \n");
this.display(arr, size);
process.stdout.write("\n");
}
}

function main()
{
var obj = new MyArray();
//Define array elements
var arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
var size = arr1.length;
obj.reverse_element(arr1, size);
//Define array elements
var arr2 = [7, 2, 9, -7, 2, 1, 8, 2, 2];
size = arr2.length;
obj.reverse_element(arr2, size);
}
main();``````

#### Output

``````Before Reversing elements
1  2  3  4  5  6  7  8
After Reversing elements
8  7  6  5  4  3  2  1

Before Reversing elements
7  2  9  -7  2  1  8  2  2
After Reversing elements
2  2  8  1  2  -7  9  2  7
``````
``````#   Python 3 Program
#   Reverse the array elements

class MyArray :
# Function which is swapping two array elements of given location
def swap_element(self, arr, i, j) :
# Get i location element
temp = arr[i]
# set new values
arr[i] = arr[j]
arr[j] = temp

# Function which is display array elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i] ," ", end = "")
i += 1

print("\n", end = "")

# Function which is Reversing given array elements
def reverse_element(self, arr, size) :
front = 0
tail = size - 1
print("Before Reversing elements \n", end = "")
self.display(arr, size)
while (front < tail) :
# Swap the array elements
self.swap_element(arr, front, tail)
front += 1
tail -= 1

print("After Reversing elements \n", end = "")
self.display(arr, size)
print("\n", end = "")

def main() :
obj = MyArray()
# Define array elements
arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
size = len(arr1)
obj.reverse_element(arr1, size)
# Define array elements
arr2 = [7, 2, 9, -7, 2, 1, 8, 2, 2]
size = len(arr2)
obj.reverse_element(arr2, size)

if __name__ == "__main__": main()``````

#### Output

``````Before Reversing elements
1    2    3    4    5    6    7    8
After Reversing elements
8    7    6    5    4    3    2    1

Before Reversing elements
7    2    9    -7    2    1    8    2    2
After Reversing elements
2    2    8    1    2    -7    9    2    7
``````
``````#   Ruby Program
#   Reverse the array elements

class MyArray

# Function which is swapping two array elements of given location
def swap_element(arr, i, j)

# Get i location element
temp = arr[i]
# set new values
arr[i] = arr[j]
arr[j] = temp
end
# Function which is display array elements
def display(arr, size)

i = 0
while (i < size)

print(" ", arr[i] ," ")
i += 1
end
print("\n")
end
# Function which is Reversing given array elements
def reverse_element(arr, size)

front = 0
tail = size - 1
# Just before the Reversing array elements
print("Before Reversing elements \n")
self.display(arr, size)
while (front < tail)

# Swap the array elements
self.swap_element(arr, front, tail)
front += 1
tail -= 1
end
# After modify array elements
print("After Reversing elements \n")
self.display(arr, size)
print("\n")
end
end
def main()

obj = MyArray.new()
# Define array elements
arr1 = [1, 2, 3, 4, 5, 6, 7, 8]
size = arr1.length
obj.reverse_element(arr1, size)
# Define array elements
arr2 = [7, 2, 9, -7, 2, 1, 8, 2, 2]
size = arr2.length
obj.reverse_element(arr2, size)
end
main()``````

#### Output

``````Before Reversing elements
1  2  3  4  5  6  7  8
After Reversing elements
8  7  6  5  4  3  2  1

Before Reversing elements
7  2  9  -7  2  1  8  2  2
After Reversing elements
2  2  8  1  2  -7  9  2  7

``````
``````/*
Scala Program
Reverse the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
def swap_element(arr: Array[Int], i: Int, j: Int): Unit = {
//Get i location element
var temp: Int = arr(i);
//set new values
arr(i) = arr(j);
arr(j) = temp;
}
//Function which is display array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i) + " ");
i += 1;
}
print("\n");
}
//Function which is Reversing given array elements
def reverse_element(arr: Array[Int], size: Int): Unit = {
var front: Int = 0;
var tail: Int = size - 1;
//Just before the Reversing array elements
print("Before Reversing elements \n");
display(arr, size);
while (front < tail)
{
//Swap the array elements
swap_element(arr, front, tail);
front += 1;
tail -= 1;
}
//After modify array elements
print("After Reversing elements \n");
display(arr, size);
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyArray = new MyArray();
//Define array elements
var arr1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8);
var size: Int = arr1.length;
obj.reverse_element(arr1, size);
//Define array elements
var arr2: Array[Int] = Array(7, 2, 9, -7, 2, 1, 8, 2, 2);
size = arr2.length;
obj.reverse_element(arr2, size);
}
}``````

#### Output

``````Before Reversing elements
1  2  3  4  5  6  7  8
After Reversing elements
8  7  6  5  4  3  2  1

Before Reversing elements
7  2  9  -7  2  1  8  2  2
After Reversing elements
2  2  8  1  2  -7  9  2  7
``````
``````/*
Swift Program
Reverse the array elements
*/
class MyArray
{
//Function which is swapping two array elements of given location
func swap_element(_ arr: inout[Int], _ i: Int, _ j: Int)
{
//Get i location element
let temp: Int = arr[i];
//set new values
arr[i] = arr[j];
arr[j] = temp;
}
//Function which is display array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i] ," ", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
//Function which is Reversing given array elements
func reverse_element(_ arr: inout [Int], _ size: Int)
{
var front: Int = 0;
var tail: Int = size - 1;
print("Before Reversing elements \n", terminator: "");
self.display(arr, size);
while (front < tail)
{
//Swap the array elements
self.swap_element(&arr, front, tail);
front += 1;
tail -= 1;
}
print("After Reversing elements \n", terminator: "");
self.display(arr, size);
print("\n", terminator: "");
}
}
func main()
{
let obj: MyArray = MyArray();
//Define array elements
var arr1: [Int] = [1, 2, 3, 4, 5, 6, 7, 8];
var size: Int = arr1.count;
obj.reverse_element(&arr1, size);
//Define array elements
var arr2: [Int] = [7, 2, 9, -7, 2, 1, 8, 2, 2];
size = arr2.count;
obj.reverse_element(&arr2, size);
}
main();``````

#### Output

``````Before Reversing elements
1    2    3    4    5    6    7    8
After Reversing elements
8    7    6    5    4    3    2    1

Before Reversing elements
7    2    9    -7    2    1    8    2    2
After Reversing elements
2    2    8    1    2    -7    9    2    7
``````

## Time Complexity

The time complexity of the algorithm depends on the number of swaps needed, which is roughly half of the array's length. Therefore, the time complexity is O(N/2), which simplifies to O(N), where N is the number of elements in the array.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post