Sorting in linear time.please spot the msitake

#include
using namespace std;

int main() {

int n;
cin>>n;

int a[n],i;

for(i=0;i<n;i++)
cin>>a[i];

int j=n-1;
int k=0;
i=0;

while(k<n)
{
if(a[k]==0)
swap(a[k],a[i++]);

else if(a[k]==2)
   swap(a[k],a[j--]);

k++;
}

for(i=0;i<n;i++)
cout<<a[i]<<endl;

return 0;

}

Hi Aashish, this program is wrong for linear sorting. You cannot sort a list in linear time by just swapping like the way you did. For Linear time sorting there are specific algorithms like Counting Sort, you may implement that.