-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDutchNationalFlag.java
More file actions
33 lines (30 loc) · 950 Bytes
/
Copy pathDutchNationalFlag.java
File metadata and controls
33 lines (30 loc) · 950 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
public class DutchNationalFlag {
public static void main(String[] args) {
int[] arr = {0,1,2, 0, 2, 1, 1, 0};
int low = 0;
int mid = 0;
int high = arr.length - 1;
while (mid <= high) {
switch (arr[mid]) {
case 0 -> {
int temp = arr[low];
arr[low] = arr[mid];
arr[mid] = temp;
low++;
mid++;
}
case 1 -> mid++;
default -> {
// arr[mid] == 2
int temp = arr[mid];
arr[mid] = arr[high];
arr[high] = temp;
high--;
}
}
}
for (int num : arr) {
System.out.print(num + " ");
}
}
}