int count[M+1]={0}; for(int i = 0; i != n; ++i) count[a[i]]++; for(int j = 0, curr = 0; j <= M; ++j) while(count[j]--) a[curr++] = j;