-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1June(IV).java
More file actions
77 lines (62 loc) · 1.61 KB
/
Copy path1June(IV).java
File metadata and controls
77 lines (62 loc) · 1.61 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class SortColors {
static void swap(int[] arr, int first, int second){
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
public static void main(String[] args) {
int[] arr = {2,0,2,1,1,0};
int n = arr.length;
// OLD APPROACh ---- not optimised
// although TC = O(n) and SC= O(1) but the code is very long
// int count0 = 0;
// int count1 = 0;
// int count2 = 0;
// for(int i : arr){
// if(i==0){
// count0+=1;
// }
// else if(i==1){
// count1+=1;
// }
// }
// for(int i =0; i<count0; i++){
// arr[i]=0;
// }
// for(int i =count0; i<count1+count0; i++){
// arr[i]=1;
// }
// for(int i =count1+count0; i<n; i++){
// arr[i]=2;
// }
// for(int i : arr){
// System.out.print(i+",");
// }
// ------------------------------------------------------------------
// optimised approach
// 3 pointer approach
// TC = O(n) and SC = (1)
int low =0, mid = 0, high = n-1;
while(mid<=high){
switch (arr[mid]) {
case 0:
swap(arr, low, mid);
low++;
mid++;
break;
case 1:
mid++;
case 2:
swap(arr, mid, high);
high--;
default:
break;
}
}
for(int x : arr){
System.out.print(x+",");
}
}
}
// -------------------------------- Output ----------------------------------
// 0,0,1,1,2,2,