Sunday, August 2, 2009

Learning Algorithm(part-2)

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:

 

  1. Does it do what we want it to do?
  2. Does it work correctly according to the original specifications of the task?
  3. Is there documentation that describes how to use it and how it works?
  4. Are procedures created in such a way that they perform logical sub functions?
  5. 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

Simple and Complete E-commerce PL/SQL Code

This is a Database of e-commerce solution. This database is quite simple but complete one.  The script (.sql file) contain all the nece...