Counting Primitive Operations

1.8k Views Asked by At

This is the solution I've been given for counting primitive Operation in an algorithm. I think I have my head around how all the operations are found, for instance the 2(n-1), the 2 is the primitive operations on that line and the n-1 is the amount of times it will be ran. I'm confused about how the final result was calculated 7n -2 and the use of brackets.

Algorithm arrayMax(A, n)                        # operations
Find Max element in an array A of n elements
currentMax <- A[0]                              2
for i <- 1 to n - 1 do                          2n
if A[i] > currentMax then                       2(n - 1)
currentMax <- A[i]                              2(n - 1)
{ increment counter i }                         n - 1
 return currentMax                              1
                                   Total        7n - 2
1

There are 1 best solutions below

1
On BEST ANSWER

$$2+2n+2(n-1)+2(n-1)+n-1+1 = \\=2+2n+2n-2+2n-2+n-1+1=\\=2n+2n+2n+n+2-2-2-1+1=7n-2$$

Is anything still unclear?