$\textbf{Just for knowledge – }$

If we remove the constraint of the $\textit{adjacent swap}$, then this problem can be solved in **$O(n)$ **with** $O(1)$ **extra space i.e, $\textit{inplace}$.

Here is the code:

class Solution { public: void Sort(vector<int>& a) { int i=0,j=a.size()-1,k=i; while(k<=j){ if(a[k]==0) swap(a[i++],a[k++]); else if(a[k]==1) k++; else if(a[k]==2) swap(a[j--],a[k]); } } };