Skip to content

Commit aa3eba7

Browse files
authored
Create Sort_Array_Of_0_1_2.java
1 parent ed2106f commit aa3eba7

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Arrays/Sort_Array_Of_0_1_2.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
import java.io.*;
4+
class GFG
5+
{
6+
public static void main (String[] args)
7+
{
8+
//code
9+
Scanner s = new Scanner(System.in);
10+
int t = s.nextInt();
11+
12+
while(t-- > 0){
13+
int n = s.nextInt();
14+
int a[] = new int[n];
15+
16+
for(int i = 0 ; i < n ; i++){
17+
a[i] = s.nextInt();
18+
}
19+
20+
int low = 0;
21+
int mid = 0;
22+
int high = n-1;
23+
int temp = -1;
24+
25+
while(mid <= high){
26+
switch(a[mid]){
27+
case 0:
28+
temp = a[low];
29+
a[low] = a[mid];
30+
a[mid] = temp;
31+
low++;
32+
mid++;
33+
break;
34+
case 1:
35+
mid++;
36+
break;
37+
case 2:
38+
temp = a[high];
39+
a[high] = a[mid];
40+
a[mid] = temp;
41+
high--;
42+
break;
43+
}
44+
}
45+
46+
for(int i=0;i<n;i++){
47+
System.out.print(a[i] + " ");
48+
}
49+
System.out.println();
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)