complexity nlog(n)