-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegregate0_1_2_array.c
More file actions
37 lines (37 loc) · 867 Bytes
/
Segregate0_1_2_array.c
File metadata and controls
37 lines (37 loc) · 867 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
34
35
36
37
#include<stdio.h>
#include<stdlib.h>
int main(){
int tt,n,*a,i,t,low,mid,high,temp;
scanf("%d",&t);
for(tt=0;tt<t;tt++){
scanf("%d",&n);
high=n-1,low=0,mid=0;
a=calloc(n,sizeof(int));
for(i=0;i<n;i++){
scanf("%d",a+i);
}
while(mid<=high){
if(a[mid]==0){
temp=a[mid];
a[mid]=a[low];
a[low]=a[mid];
low+=1;
mid+=1;
}else if(a[mid]==1){
mid+=1;
}else if(a[mid]==2){
temp=a[mid];
a[mid]=a[high];
a[high]=temp;
high-=1;
if(mid==high){
mid+=1;
}
}
}
for(i=0;i<n;i++){
printf("%d ",a[i]);
}
printf("\n");
}
}