Data structure and algorithms time computation?

AI Thread Summary
The discussion revolves around analyzing the time complexity of a given set of instructions, specifically focusing on the worst-case scenario. The initial analysis presents a formula for total execution time, which includes constants A and B. A key point of confusion is clarified: the condition should be changed from "i < n" to "i >= n" for correct functionality. Additionally, if n equals 1, the first execution of line 2 will lead to an immediate return since the condition will not be satisfied. The conversation emphasizes the importance of accurately defining loop conditions in algorithm analysis.
mohamed el teir
Messages
88
Reaction score
1
Moved from a technical forum, so homework template missing.
Assume the following set of instructions:

1. i = 0
2. if i < n, goto line 6
3. if A [ i ] = = x, goto line 7
4. i++
5. goto line 2
6. return false
7. return true

Assume that line i take Ci time, where Ci is a constant. The worst case total time of running this block of code can be calculated as: C1 + (n+1)C2 + nC3 + nC4 + nC5 + C6 = (C2 + C3 + C4 + C5)n + (C1 + C2 + C6) = An + B, where A = C2 + C3 + C4 + C5 and B = C1 + C2 + C6 are both constants.

is this right ?
 
Last edited by a moderator:
Physics news on Phys.org
If n is 1 what will happen the first time line 2 is executed? Fix this so it works as intended and the rest of the answer is OK.

Please use a homework forum and template for homework or similar questions.
 
sorry it is supposed to be i >= n not i < n, regarding the homework thing : i didn't get this topic from college but from this site https://www.cpp.edu/~ftang/courses/CS240/lectures/analysis.htm while i was reading about algorithms. i saw that somethings in it are not right so i made some changes to it and asked about it after changing. sorry for any inconvenience
 
Back
Top