-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigZagArray.java
More file actions
executable file
·53 lines (47 loc) · 1.4 KB
/
ZigZagArray.java
File metadata and controls
executable file
·53 lines (47 loc) · 1.4 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MultiHashtable;
import java.util.Map;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Neel_Kapadia
*/
public class ZigZagArray {
int[] rearrange(int array[]) {
boolean flag;
int temp;
for (int i = 0; i < array.length - 1; i++) {
if(i%2==0){
flag = true;
}
else{
flag = false;
}
if (flag == true) {
if (array[i] > array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
} else {
if (array[i] < array[i + 1]) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
return array;
}
public static void main(String[] args) {
ZigZagArray z = new ZigZagArray();
int [] array = {4, 3, 7, 8, 6, 2, 1};
int arr[] = z.rearrange(array);
for(int i = 0 ; i < arr.length; i++){
System.out.println(arr[i]);
}
}
}