Would be good to have a note about the algorithmic complexity of the code. Is it O(n^2) in the worst case?