What is meant by STEP-COUNT METHOD?
You define what a "step" means for the algorithm (usually statements), then the total of those steps using variables such as N can be calculated.What is the procedure in that to calculate?First define steps:
int mean(int a[], size_t n)
{
int sum = 0; // 1 step
for (int i = 0; i < n; i++) // 1 step
sum += a[i]; // 1 step
return sum; // 1 step
}
Next determine the frequency of the steps based on N:
int mean(int a[], size_t n)
{
int sum = 0; // 1 step * 1
for (int i = 0; i < n; i++) // 1 step * (N+1)
sum += a[i]; // 1 step * N
return sum; // 1 step * 1
}
Add up the steps:1 + (N+1) + N + 1
Reduce: 2N + 3
Throw away factors that don't grow with N and you're done: O(N) Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401