The following algorithm finds and returns the maximum of n given numbers:
Algorithm Max (A, n)
// A is an array of size n.
{
Result: = A [1];
For I : = 2 to n do
If A [i] > Result then Result : = A [i]
Return Result;
}
In this algorithm (named Max), A and n are procedure parameters, Result and I are local variables.
Performance Analysis:
There are many criteria upon which we can judge an algorithm. For instance:
- Does it do what we want it to do?
- Does it work correctly according to the original specifications of the task?
- Is there documentation that describes how to use it and how it works?
- Are procedures created in such a way that they perform logical sub functions?
- Is the code readable?
Space/Time Complexity:
The space complexity of an algorithm is the amount of memory it needs to run to completion. The time complexity of an algorithm is the amount of computer time it needs to run to completion.
The following algorithm is Iterative function for sum:
Algorithm Sum (a, n)
{
s : = 0.0;
for i : = 1 to n do
s : = s + a[i];
return s;
}
No comments:
Post a Comment