java - What is the big o estimate of this nested for loop? -
this question has answer here:
i trying figure out big o estimate of following nested loop is.
for(int = 10; 100; i++){ for(int j = 1; j i; j++){ for(int k = 1; k j; k++){ s=s+i+j; } } }
i thinking 2 inner loops run n times unsure first loop.
what big o estimate code?
technically complexity o(1)
because 100
constant , number of operations same. if author assumed 100
parameter , can variable, i.e.
int n = 100; //or read somewhere (int = 10; < n; ++i) { .... }
then complexity o(n^3)
. first cycle body executed n - 10
times o(n)
, second cycle's body executed n - 10
times once, again o(n)
, , same can stated innermost cycle, giving o(n^3)
.
article useful understanding big o notation: big o notation
Comments
Post a Comment