```
// A[0]...A[l0] are all 0, A[l0+1]...A[l1] are all 1, A[l1+1]... are all 2,
//if A[i]==0 then we need to check if 1 has appeared or not by if(l1>=l0)
void sortColors(int A[], int n) {
int l0=-1, l1=-1;
for(int i=0; i<n; ++i){
if(A[i]==1) swap(A[++l1],A[i]);
if(A[i]==0){
swap(A[++l0],A[i]);
if(l1>=l0) swap(A[++l1],A[i]);
else l1=l0;
}
}
}
```