How to reverse elements in Array

The following illustrates reversing elements in an array using the common swapping technique. The logic is dead simple and is considered to be of easy to medium difficulty. In this I will provide an example illustrating the logic with explanation.




import java.util.*;
class ReverseArray
{
    public static void main(String args[])
    {

    // Create java.util.Scanner object for taking input
    Scanner s=new Scanner(System.in);
   
   
    // Take no.of elements and store it in n
    System.out.println("Enter the no.of elements");
    int n=s.nextInt();
   
    // Create array of size n
    int a[]=new int[n];

    // Read elements into the array
    System.out.println("Enter the elements into the array");
        for(int i=0;i<n;i++)
        {
        a[i]=s.nextInt();
        }

    // Reverse elements in the array
    reverse(a);

        // Print the array
        for(int i=0;i<n;i++)
        {
        System.out.printf("a[%d]=%d\n",i,a[i]);
        }
    }

    public static void reverse(int[] a)
    {
   
// Loop for length/2 times only else re-swapping takes place
        for(int i=0;i<a.length/2;i++)
        {
        int temp=a[i];
        a[i]=a[(a.length-1)-i];
        a[(a.length-1)-i]=temp;
        }

    }
}

Sample outputs of the program


1.

Enter the no.of elements
5
Enter the elements into the array
1
2
3
4
5
a[0]=5
a[1]=4
a[2]=3
a[3]=2
a[4]=1

2.

Enter the no.of elements
4
Enter the elements into the array
1
2
3
4
a[0]=4
a[1]=3
a[2]=2
a[3]=1

Explaining the logic for reversing array

I think you might be familiar with all the other code written except the logic. So new let's get into the logic.

for(int i=0;i<a.length/2;i++) The loop has to be rotated half of the length of the array times. For instance, if the length of the array is 4 then the loop has to be rotated 2 times,  if 11 then 5 times (you get 5.5 but rounded to 5). This is necessary because when you loop till the length of the array times then re swapping takes place i.e. you'll find no change in the array. You get the same output. Try it yourself for a better understand.

Swapping starts..
int temp=a[i]; Take the element at index i and store it in a temp variable.

a[i]=a[(a.length-1)-i]; Now replace the element at i index with the element at ith index from last. Consider a case, when the loop rotates for the first time, the value of i will be 0 and so element at index 0 will be replaced with the last element i.e. the element at the last index.

(a.length-1)-i; (You do not need brackets here). When this statement executes for the first time i.e. when i=0 and consider a.length=5 (as in the first case). Then (a.length-1)-i will become 5-1-0=4. So a[(a.length-1)-i] you get a[4]. So a[0]=a[4]

For the next time when i=1, you get 5-1-1=3. So a[1]=a[3]
When i=2 then what happens? The loop condition is not satisfied because i is not less than 2 (a.length/2=(5/2)=2.5~2)

a[(a.length-1)-i]=temp; Till now you have replaced left elements with the right ones. Now the right ones should get the value of the left ones. So we write a[(a.length-1)-i]=temp; where temp contains the value of the corresponding left ones. That's it!

In this way, we can write an easy logic for reversing the elements in array using the swapping technique.

No comments: