O(n^2) for the entire code.
More specifically, the test++
will be executed about (n-1)*n/2 times. But that still is called "O(n^2)" or "quadratic".
Big-O ignores any constant multiplier -- 1/2 in this case.
Also, it ignores any lesser components. Note (n-1)n/2 = (nn - n)/2. So the n*n
is used and the n
is ignored.
That particular bit of code is not very practical -- the for
looping will take longer than ++
. Furthermore, a good optimizer might run find some way to optimize it.
You said "the worst runtime". That is not really valid. It could be that the "worst" only occurs very infrequently. That is, you might end up with only O(n log n) in some other situation.
Another way to think through the issue: O(n^2) means that doubling n will approximately quadruple the execution time.