Yes, quadratic, but how many cells do you think can appear in a bag? Besides, there are terms other than cells in the bag, I don't see any benefit to write more complicated code for this.