Understanding integer factorization is NP
The flaw in your reasoning is in considering $n$ as the size of the input.
The input is a number $n$ and its size in memory is $\lfloor \log_2(n) + 1 )\rfloor$ bits.

Thus, $O(\sqrt n)$ is not polynomial in the size of the input.  

In order to be polynomial, you would need an algorithm with time complexity $O(\log^k n)$ which would correspond to a polynomial algorithm of order $k$ w.r.t. the size of the input.