Java program to reverse array using temporary variable
Program
import java.util.Scanner;
public class ReverseArrayUsingTempVariable {
private void reverseArray(int arr[], int size) {
int temp;
int start = 0;
int end = size-1;
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
}
public static void main(String[] args) {
int i = 0;
System.out.println("Enter the required size of the array: ");
Scanner reader = new Scanner(System.in);
int size = reader.nextInt();
int inputArray[] = new int[size];
System.out.println("Enter the elements of the array: ");
for (i = 0; i < size; i++) {
inputArray[i] = reader.nextInt();
}
System.out.println("Array in reverse order: ");
ReverseArrayUsingTempVariable reverseArrayUsingTempVariable = new ReverseArrayUsingTempVariable();
reverseArrayUsingTempVariable.reverseArray(inputArray,size);
}
}
This program demonstrates how to reverse the elements of an array by swapping elements using a temporary variable.
-
Class and Method:
- A class
ReverseArrayUsingTempVariable
is created with a methodreverseArray(int arr[], int size)
to reverse the array.
- A class
-
Reversal Logic:
- Two pointers,
start
andend
, are initialized to the first and last indices of the array, respectively. - A
while
loop is used to iterate until thestart
pointer is less than theend
pointer. - In each iteration:
- The element at
start
is swapped with the element atend
using a temporary variabletemp
. - The
start
pointer is incremented, and theend
pointer is decremented.
- The element at
- Two pointers,
-
Printing the Reversed Array:
- After the loop completes, the reversed array is printed using a
for
loop.
- After the loop completes, the reversed array is printed using a
-
Main Method:
- The program reads the size of the array and its elements from the user.
- It creates an instance of the class and calls the
reverseArray
method with the input array and its size.
Example:
Input:
- Array size:
5
- Array elements:
10 20 30 40 50
Process:
- Initially:
start = 0
,end = 4
- Swap
arr[0]
andarr[4]
:50 20 30 40 10
- Increment
start
to1
and decrementend
to3
. - Swap
arr[1]
andarr[3]
:50 40 30 20 10
start
becomes2
andend
becomes2
, ending the loop.
Output:
- Reversed array:
50 40 30 20 10
Key Features:
- In-Place Reversal:
- The array is reversed without using additional data structures, making it memory-efficient.
- Efficient:
- The time complexity is
O(n)
, as the loop iterates through half of the array.
- The time complexity is
- Minimal Memory Usage:
- The program uses a single temporary variable for swapping.
- Reusable Method:
- The
reverseArray
method can be reused for reversing any integer array.
- The
This program demonstrates a simple and efficient way to reverse an array using temporary variables and two-pointer logic.
Output
javac .\ReverseArrayUsingTempVariable.java
java ReverseArrayUsingTempVariable
Enter the required size of the array:
5
Enter the elements of the array:
11
22
33
44
55
Array in reverse order:
55 44 33 22 11