Algorithms
An algorithm is a set of well defined steps for solving a problem. The algorithm indicates the control flow of the solution, or what steps are repeated and where conditions must be tested to indicate which code to execute next in the solution of a problem. There may be many different algorithms for solving the same problem. Algorithms that solve the same problem are often compared to each other to choose the best algorithm to implement. Some criteria for comparing algorithms are:
- number of steps (lines of code)
- difficulty (time) of implementing
- type and amount of memory or storage space required
- execution time on target machine
- ease of maintenance of implemented version
Elegant algorithms are algorithms that would score high on most if not all of the above criteria. Professional programmers understand which criteria are most important for their particular program tasks and their experience helps them choose the algorithm that best meets their needs. As novice programmers, it is important to learn to identify different algorithms and to be able to evaluate the efficiency of each algorithm before choosing which algorithm to implement. Then, implement the best algorithm you know for your particular problem and continue to learn new algorithms as you gain experience.
We often describe algorithms using pseudo-code. Pseudo-code is English (at least for us) that looks a little like program code. It is easier to understand than program code and it better illustrates the step-by-step nature of a solution than paragraphs written in English.
Example: Pseudo-code algorithm for calculating several persons' BMI
- Store the number of persons (can get from user input or read from file, etc.)
- Store a value that indicates if the data will be entered in standard or metric units.
- For each person:
- Store the height of the person.
- Store the weight of the person.
- If data is in standard units:
- convert values to metric units.
- Calculate the BMI as weight / height2.
- Store the results.
- [Optional] Display the results to the user as program runs.
- [Optional] Write results to a file.
Once we have an algorithm for solving a problem, we can implement (code) it in Matlab or any other suitable programming environment.
Algorithms that solve complex problems like network traffic flow, and circuit design are much more complex then a single list of steps. They have many conditional and iterative statements and may be broken into smaller functions of a much larger solution.
For more information about algorithms, read the
http://en.wikipedia.org/wiki/Algorithm entry.