c - Big O of nested loop that with varying iterations? -


i'm having bit of trouble figuring out big o run time 2 set of code samples iterations depend on outside loops. have basic understanding of big o run times , can figure out run times simpler code samples. i'm not sure how lines affecting run time.

i consider first 1 o(n^2). however, i'm not certain.

for(i = 1; < n; i++){  for(j = 1000/i; j > 0; j--){  <--not sure if still o(n)     arr[j]++; /* line */  } } 

i'm bit more lost one. o(n^3) possibly o(n^2)?

for(i = 0; < n; i++){  for(j = i; j < n; j++){     while( j<n ){       arr[i] += arr[j]; /* line */       j++;     }  } } 

i found post , applied first code sample i'm still unsure second. what big-o of nested loop, number of iterations in inner loop determined current iteration of outer loop?

for second loop (which appears still need answer for), have sort of misleading bit of code, have 3 nested loops, @ first glance, makes sense runtime o(n^3).

however, incorrect. because innermost while loop modifies j, same variable loop modifies. code equivalent bit of code below:

for(i = 0; < n; i++){   for(j = i; j < n; j++){     arr[i] += arr[j]; /* line */     j++;   } } 

this because while loop on inside run, incrementing j until j == n, breaks out. @ point, inner loop increment j again , compare n, find j >= n, , exit. should familiar case already, , recognize o(n^2).

just note, second bit of code not safe (technically), j may overflow when increment additional time after while loop finishes running. cause loop run forever. however, occur when n = int_max().


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -