In total, we are using only time O(n log n) time.