c++ - What is the correct Big O of this code -
i'm trying learn big o notation , i'm little confused c++ code:
void enterelements(int *s1, int s1size) { for(int x = 0;x < s1size;++x) { retry: cout<<"element "<<x + 1<<": "; cin>>s1[x]; int valid = validation(); if(valid == 1) { cout<<"the input must numbers."<<endl; goto retry; } } }
because don't know how got 3 results:
- 9n + 1 -> o(n)
- 7nm + 2m + 2n + 1 -> o(nm)
- 7n^2 + 4n + 1 -> o(n^2)
is of correct? if not, can me find correct answer?
int validation() { int validation = 0; if(cin.fail()) { validation = 1; cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); } else validation = 0; return validation; }
big-oh notation isn't applicable here. have lower bound, there absolutely no gurantees on validation(), big-oh designation o(inf), that's quite unhelpful.
your code (should validations go properly), be:
Ω(s1size)
because executed s1size
times, not less. big-oh notation not lower bounds. since have no guarantee on how many times goto
statement used, , therefore no upper bound, no applicable big-oh derivation.
your runtime, in simple terms: greater or equal s1size iterations (barring error causes loop exit).
thus best case above, , worst case, well, forever!
edit: Ω correct here, not ω, Ω implies runtime greater or equal s1size
Comments
Post a Comment