# WAP to Swap Two Adjacent Elements in an Array

What We Are Learn:

Swap Two Adjacent Elements in an Array: This Java program takes an input array containing integers and swaps adjacent elements to create a modified array. The program achieves this by iterating through the array with a step size of 2 (i.e., taking every second element), and for each pair of adjacent elements, it swaps their positions.

### Swap Two Adjacent Elements in an Array

```package com.softwaretestingo.interviewprograms;
public class InterviewPrograms33
{
/*
* Input array- {1,2,3,4,5}
* Output array- {2,1,4,3,5}
*/
public static void main(String[] args)
{
int[] arr = {1,2,3,4,5};
int tmp=0;
for(int i =1;i<arr.length;i+=2)
{
tmp=arr[i];
arr[i]=arr[i-1];
arr[i-1]=tmp;
}
for(int i:arr)
System.out.print(i);
}
}```

Output

`21435`

Here’s a breakdown of the program:

1. Define the main method:
• The entry point of the program is the main method.
2. Initialize the input array:
• The input array arr is initialized with the values {1, 2, 3, 4, 5}.
• The program iterates through the array with a for loop, starting from index 1 (the second element) and incrementing by 2 at each step.
• For each iteration, it takes the current element arr[i] and swaps its position with the previous element arr[i-1] using a temporary variable tmp.
4. Print the modified array:
• After processing all elements of the array and performing the swaps, the program prints the modified array elements.

Overall, this program swaps adjacent elements of the input array to create a modified array. For the given input array {1, 2, 3, 4, 5}, the output will be {2, 1, 4, 3, 5}, where adjacent elements have been swapped. The program demonstrates basic array manipulation and a simple for loop to achieve this task.

Alternative Way 1:

This Java program takes an input array containing integers and swaps adjacent elements to create a modified array. The program achieves this using a for loop and conditional statements to handle arrays of even and odd lengths.

```package com.softwaretestingo.interviewprograms;
import java.util.Arrays;
public class InterviewPrograms33_1
{
/*
* Input array- {1,2,3,4,5}
* Output array- {2,1,4,3,5}
*/
public static void main(String[] args)
{
int [] a = {1,2,3,4,5};
if ( a.length % 2 == 1 )
{
for ( int i = 0 ; i<a.length-1; i=i+2)
{
int temp= a[i];
a[i]= a[i+1];
a[i+1]=temp;
}
}
else if (a.length%2==0)
{
for (int i=0; i<a.length; i=i+2)
{
int temp= a[i];
a[i] = a[i+1];
a[i+1]= temp ;
}
}
System.out.println(Arrays.toString(a));
}
}```

Output

`[2, 1, 4, 3, 5]`

Here’s a breakdown of the program:

1. Define the main method:
• The entry point of the program is the main method.
2. Initialize the input array:
• The input array a is initialized with the values {1, 2, 3, 4, 5}.
3. Swap adjacent elements for arrays with odd length:
• The program first checks if the length of the array a is odd (i.e., a.length % 2 == 1).
• If the array length is odd, it means the last element is not paired with another element for swapping, so it iterates through the array from the beginning to the second-to-last element using a for loop with a step size of 2.
• For each iteration, it swaps the current element a[i] with the next element a[i+1] using a temporary variable temp.
4. Swap adjacent elements for arrays with even length:
• If the array length is even (i.e., a.length % 2 == 0), it means all elements are paired for swapping, so it iterates through the array from the beginning to the last element using a for loop with a step size of 2.
• It swaps the current element a[i] with the next element a[i+1] using a temporary variable temp.
5. Print the modified array:
• After processing all elements and performing the swaps, the program prints the modified array using Arrays.toString(a).

Overall, this program swaps adjacent elements of the input array to create a modified array. For the given input array {1, 2, 3, 4, 5}, the output will be {2, 1, 4, 3, 5}, where adjacent elements have been swapped. The program demonstrates basic array manipulation, conditional statements, and loops to achieve this task. I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.