-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseArray.java
More file actions
37 lines (31 loc) · 939 Bytes
/
reverseArray.java
File metadata and controls
37 lines (31 loc) · 939 Bytes
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
import java.util.Arrays;
public class reverseArray {
public static void main(String[] args) {
int[] arr={10,20,30,40,50};
System.out.println(Arrays.toString(arr));
reverse(arr);
// System.out.println(Arrays.toString(arr));
// System.out.println(arr);
System.out.println(Arrays.toString(arr));
}
// static void reverse(int[] arr){ using FOR LOOP
// for(int i=arr.length-1;i>=0;i--){
// System.out.println((arr[i]));
// }
// }
static void reverse(int[] arr){
int start=0;
int end =arr.length-1;
while(start<end){
swap(arr,start,end);
start++;
end--;
}
//System.out.println(arr[i]);
}
static void swap(int[] arr,int index1,int index2){
int temp=arr[index1];
arr[index1]=arr[index2];
arr[index2]=temp;
}
}