forked from souvikg544/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort012java.java
More file actions
44 lines (39 loc) · 1.04 KB
/
sort012java.java
File metadata and controls
44 lines (39 loc) · 1.04 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
public class sort012java {
public static void main(String args[])
{
sortColors(new int[]{ 1,0,0,2,2,1,0,2,0,1});
}
public static void sortColors(int[] nums) {
int lo = 0;
int hi = nums.length - 1;
int mid = 0;
int temp;
while (mid <= hi)
{
switch (nums[mid]) {
case 0: {
temp = nums[lo];
nums[lo] = nums[mid];
nums[mid] = temp;
lo++;
mid++;
break;
}
case 1:
mid++;
break;
case 2: {
temp = nums[mid];
nums[mid] = nums[hi];
nums[hi] = temp;
hi--;
break;
}
}
}
for (int i = 0; i < nums.length; i++)
{
System.out.println(nums[i]);
}
}
}