Given an input integer array arr, we want to reverse the order of the elements. The first element should become last element. The second element should become second last element and so on.
|
This algorithm performs an in-place reversal of an array by iterating to its midpoint and using a temporary variable to swap symmetric elements from the beginning and end.
|
| The java method should accept following input parameters: arr (int array).
|
| Initialize integer variable endIdx to (arr.length-1) (this is the last index of the array).
|
| Iterate through arr using a for loop, using variable startIdx as loop counter. Start value of startIdx is 0 and end value of startIdx should be less than (arr.length/2). As we want to reverse the array, when we reach (arr.length/2), the reversal process is complete.
Interchange the array elements at index startIdx and index endIdx using a temporary variable temp;
Decrement endIdx by 1.
|
| At the end of the for loop execution, return the reversed array.
|
This execution trace will help you understand the code better.
For this execution, the input array is arr={1,2,3,4,5,6,7,8}.
| Step | startIdx | endIdx | temp | Action | Array State |
| Initial | - | 7 | 0 | Initialize variables | [1, 2, 3, 4, 5, 6, 7, 8] |
| Iteration 1 | 0 | 7 | 1 | Swap arr[0] and arr[7] | [8, 2, 3, 4, 5, 6, 7, 1] |
| Iteration 2 | 1 | 6 | 2 | Swap arr[1] and arr[6] | [8, 7, 3, 4, 5, 6, 2, 1] |
| Iteration 3 | 2 | 5 | 3 | Swap arr[2] and arr[5] | [8, 7, 6, 4, 5, 3, 2, 1] |
| Iteration 4 | 3 | 4 | 4 | Swap arr[3] and arr[4] | [8, 7, 6, 5, 4, 3, 2, 1] |
| End | 4 | 3 | 4 | Loop terminates (4 < 4 is false) | [8, 7, 6, 5, 4, 3, 2, 1] |
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class ArrayReversal {
public int[] arrayReversal(int[] arr) throws Exception{
int endIdx=arr.length-1;
int temp=0;
for (int startIdx=0; startIdx < arr.length/2; startIdx++) {
temp=arr[startIdx];
arr[startIdx]=arr[endIdx];
arr[endIdx]=temp;
endIdx--;
}
return arr;
}
public static void main(String[] args) {
ArrayReversal ap=new ArrayReversal();
try {
int[] intArray1 = {1,2,3,4,5,6,7,8};
printArraySummary(intArray1, "Original Array:");
intArray1=ap.arrayReversal(intArray1);
printArraySummary(intArray1, "Reversed Array:");
int[] intArray2 = {-3,-2,-1,0,1,2,3};
printArraySummary(intArray2, "Original Array:");
intArray2=ap.arrayReversal(intArray2);
printArraySummary(intArray2, "Reversed Array:");
int[] intArray3 = {4,5};
printArraySummary(intArray3, "Original Array:");
intArray3=ap.arrayReversal(intArray3);
printArraySummary(intArray3, "Reversed Array:");
int[] intArray4 = {4,4};
printArraySummary(intArray4, "Original Array:");
intArray4=ap.arrayReversal(intArray4);
printArraySummary(intArray4, "Reversed Array:");
int[] intArray5 = {1};
printArraySummary(intArray5, "Original Array:");
intArray5=ap.arrayReversal(intArray5);
printArraySummary(intArray5, "Reversed Array:");
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
}
}
public static void printArraySummary(int[] intArray, String label) throws Exception {
// Case 1: The input Array is null !!
if (intArray == null) { System.out.println("\n\n Input Array was null !! \n"); return; }
// Case 2: Print input Array by index (first to last)
System.out.println();
System.out.println("************************************************************************");
System.out.print(label+" : ");
int arrayIndex=0;
for (arrayIndex=0; arrayIndex< intArray.length; arrayIndex++) {
System.out.print(intArray[arrayIndex]);
if (arrayIndex< intArray.length-1) {System.out.print(",");}
}
System.out.println();
System.out.println("*************************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
}