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.

  1. Class and Method:

    • A class ReverseArrayUsingTempVariable is created with a method reverseArray(int arr[], int size) to reverse the array.
  2. Reversal Logic:

    • Two pointers, start and end, are initialized to the first and last indices of the array, respectively.
    • A while loop is used to iterate until the start pointer is less than the end pointer.
    • In each iteration:
      • The element at start is swapped with the element at end using a temporary variable temp.
      • The start pointer is incremented, and the end pointer is decremented.
  3. Printing the Reversed Array:

    • After the loop completes, the reversed array is printed using a for loop.
  4. 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] and arr[4]: 50 20 30 40 10
  • Increment start to 1 and decrement end to 3.
  • Swap arr[1] and arr[3]: 50 40 30 20 10
  • start becomes 2 and end becomes 2, ending the loop.

Output:

  • Reversed array: 50 40 30 20 10

Key Features:

  1. In-Place Reversal:
    • The array is reversed without using additional data structures, making it memory-efficient.
  2. Efficient:
    • The time complexity is O(n), as the loop iterates through half of the array.
  3. Minimal Memory Usage:
    • The program uses a single temporary variable for swapping.
  4. Reusable Method:
    • The reverseArray method can be reused for reversing any integer array.

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