What is the complexity of this algorithm?