-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwap.java
More file actions
37 lines (30 loc) · 1.01 KB
/
Copy pathSwap.java
File metadata and controls
37 lines (30 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package Array;
import java.util.Arrays;
public class Swap {
public static void main(String[] args) {
int[] arr = {1, 2, 23, 9, 18};
swap(arr, 1, 3);
System.out.print("Array after swapping 1st & 3rd index value: ");
System.out.println(Arrays.toString(arr));
System.out.print("Reversed Array: ");
//here we have reversed the swapped array not the original array.
reverse(arr);
System.out.println(Arrays.toString(arr));
}
// creating swap method that is used in psvm.
static void swap(int[] arr, int index1, int index2){
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
static void reverse(int[] arr){
int start = 0;
int end = arr.length-1;
while (start<end){
swap(arr, start, end);
start++;
end--;
} // works both for even no. of array and odd no. of arrays
// we are using two pointer here.
}
}