Leonardo Lopez
Leonardo Lopez

Reputation: 1143

How to analyze the efficiency of this algorithm Part 2

I found an error in the way I explained this question before, so here it goes again:

FUNCTION SEEK(A,X)
1. FOUND = FALSE
2. K = 1
3. WHILE (NOT FOUND) AND (K < N)
   a.  IF (A[K] = X THEN
       1.  FOUND = TRUE
   b.  ELSE
       1.  K = K + 1
4. RETURN

Analyzing this algorithm (pseudocode), I can count the number of steps it takes to finish, and analyze its efficiency in theta notation, Θ(n), a linear algorithm. OK.

This following code depends on the inner formulas inside the loop in order to finish, the deal is that there is no variable N in the code, therefore the efficiency of this algorithm will always be the same since we're assigning the value of 1 to both A & B variables:

1.  A = 1
2.  B = 1
3.  UNTIL (B > 100)
    a.  B = 2A - 2
    b.  A = A + 3

Now I believe this algorithm performs in constant time, always. But how can I use Algebra in order to find out how many steps it takes to finish?

Upvotes: 0

Views: 160

Answers (1)

alanmanderson
alanmanderson

Reputation: 8210

Yes this second part runs in O(1) time. The only argument you need to prove that is that everytime it will iterate a set number of times.

Algebraically A is growing by 3 everytime so B is growing by 6 every time. It will execute roughly 100/6 times.

Upvotes: 2

Related Questions