-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigZag.java
More file actions
30 lines (29 loc) · 826 Bytes
/
Copy pathZigZag.java
File metadata and controls
30 lines (29 loc) · 826 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
package Arrays;
public class ZigZag {
public static void main(String[] args) {
int arr[] = {4,3,7,8,6,2,1};
boolean flag = true;
int temp = 0;
for(int i = 0; i < arr.length-1; i++) {
if(flag) {
if(arr[i] > arr[i+1]) {
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
else {
if(arr[i] < arr[i+1]) {
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
flag = !flag;
}
System.out.println("Zig Zag Array : ");
for(int i : arr) {
System.out.print(i + ",");
}
}
}