algorithm - Compute running time function -
i have method , want compute running time:
print() { node* p1 = sentinel_->next_; while(p1 != sentinel_) cout << p1->data_ << “ “; p1 = p1->next_; } cout << endl; }
if assumed while
loop can executed n-1 times. so: t(n) = 1 + (n-1) + (n-1) + (n-1) + 1 = 3n - 1. correct value use “input size,” n? gonna based on t(n) 3n - 1 >= 0 n >= 1/3 or n greater or equal 1 since while loop can @ least executed 1 time.
the algorithmic complexity calculated in terms of asymptotic notation. means how algorithm behaves size of input varies.
for example, "n" anything. if n == 2, while loop run once, if n == 3, while loop runs twice...in general, mentioned while loop runs "n-1" number of times given value of "n". means value of "n" increases, while loop take longer run. thus, complexity of algorithm directly determined number of times loop run. , number of times loop run determined value of "n". thus, code has running complexity of o(n).
let's have method first traverse list , add 10 each node value. then, again traverse list print each node value. algorithm have running complexity of o(n) + o(n) = 2 * o(n). still equates o(n) runtime since constants ignored. again, theoretically. practically, looping on list twice going make program slower.
i hope clear in explanation. not using technical terms hope point.
Comments
Post a Comment