Hacker News new | ask | show | jobs
by jmreardon 5428 days ago
Big O notation is intended to convey the algorithm's runtime as a function of the inputs. By the sounds of it, the number of checkboxes you had was actually fixed and relatively small. In this case, the algorithm is still O(n). The real problem with your code is that you went through the list (an expensive operation) multiple times. This gives you a large constant factor, not O(n^2). Because Big O notation is only concerned with large (asymptotic) values, the constants are generally ignored.

If you had a form where the number of checkboxes scaled with the number of items on the list, then you might end up with an O(n^2) algorithm, depending on how exactly they scale.

1 comments

You're right - it wasn't O(n^2); the number of operations was (Number of checkboxes) x (Number of products). So with 10 checkboxes and 200 products, I had 2000 operations. 201 products = 2010 operations.

My problem would have been N^N if I had been doing "for every checkbox... for every checkbox" instead of "for every checkbox... for every product..."

So it wasn't n^2, but it was scaling poorly.

The way I rewrote it, I had (Number of products) operations. So for 200 products, I had 200 operations.

You can see that this made it much more tenable to add 100 more products, even if it wasn't N^2.