Reversing the order of elements in a Java array

In this blog post, we will discuss how to reverse the order of elements in a Java array. Reversing the order of an array is a common programming task and can be useful in many scenarios. We will explore different approaches to achieve this and provide example code along the way.

Table of Contents

Approach 1: Using a Auxiliary Array

One simple approach to reverse an array is to create a new auxiliary array of the same size and copy the elements from the original array in reverse order. Here’s an example implementation:

public static void reverseArray(int[] arr) {
    int[] reversed = new int[arr.length];
    int j = arr.length - 1;
    for (int i = 0; i < arr.length; i++) {
        reversed[j] = arr[i];
        j--;
    }
    System.arraycopy(reversed, 0, arr, 0, arr.length);
}

To reverse the elements of an array, we create a new array of the same size as the original array. We iterate over the original array from start to end and copy each element to the corresponding index in the reversed array in reverse order. Finally, we use System.arraycopy() method to copy the reversed array back to the original array.

Approach 2: Using Two Pointers

Another approach to reverse an array is by using two pointers, one starting from the beginning of the array and the other starting from the end. We swap the elements at these two pointers and continue moving them towards the center until they meet. Here’s an example implementation:

public static void reverseArray(int[] arr) {
    int start = 0;
    int end = arr.length - 1;
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

This approach avoids the need for an auxiliary array and swaps the elements in-place by constantly exchanging the elements at the two pointers.

Approach 3: In-place Reversal

If we are allowed to modify the original array itself, we can perform an in-place reversal of the array using a single pointer. We iterate over the array up to its midpoint and swap each element with its corresponding element from the end of the array. Here’s an example implementation:

public static void reverseArray(int[] arr) {
    int mid = arr.length / 2;
    for (int i = 0; i < mid; i++) {
        int temp = arr[i];
        arr[i] = arr[arr.length - 1 - i];
        arr[arr.length - 1 - i] = temp;
    }
}

This approach reduces the space complexity to O(1) as it does not require any extra memory.

Conclusion

Reversing the order of elements in a Java array can be done using various approaches. Depending on the requirements and constraints of your application, you can choose the most suitable approach. In this blog post, we explored three different methods: using an auxiliary array, using two pointers, and performing in-place reversal. Each approach has its own advantages and can be used in different scenarios.