-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion4.java
More file actions
40 lines (37 loc) · 1.11 KB
/
question4.java
File metadata and controls
40 lines (37 loc) · 1.11 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
public class question4 {
public static void main(String[] args) {
int[] a = {2, 0, 1, 2, 0, 1};
System.out.println("Original Array:");
for (int i : a) {
System.out.print(i + " ");
}
sortColors(a);
System.out.println("\nSorted Array:");
for(int i : a) {
System.out.print(i + " ");
}
}
static void sortColors(int[] a) {
int low = 0, mid = 0, high = a.length - 1;
while(mid <= high){
switch(a[mid]){
case 0:
int temp0 = a[low];
a[low] = a[mid];
a[mid] = temp0;
low++;
mid++;
break;
case 1:
mid++;
break;
case 2:
int temp2 = a[high];
a[high] = a[mid];
a[mid] = temp2;
high--;
break;
}
}
}
}