Computer >> Computer tutorials >  >> Programming >> Programming

Step Count Method in Algorithm


The step count method is one of the method to analyze the algorithm. In this method, we count number of times one instruction is executing. From that we will try to find the complexity of the algorithm.

Suppose we have one algorithm to perform sequential search. Suppose each instruction will take c1, c2, …. amount of time to execute, then we will try to find out the time complexity of this algorithm

AlgorithmNumber of timesCost
seqSearch(arr, n, key)
i := 0
while i < n, do
   if arr[i] = key, then
      break
   end if
done
return i
1
n+1
n
0/1



1
c1
c2
c3
c4



c5

Now if we add the cost by multiplying the number of times it is executed, (considering the worst case situation), we will get

                  Cost=c1+(n+1)c 2+nc3+c 4+c 5

                  Cost=c1+nc 2+c2+nc 3+c 4+c5

                  Cost=n(c 2+c3)+c 1+c 4+c5

                  Cost=n(c 2+c3)+C

Considering the c1 + c4 + c5 is C, so the final equation is like straight line y = mx + b. So we can say that the function is linear. The complexity will be O(n).