ALGORITHM: COMPLETE NOTES
Definition of Algorithm:
An algorithm is a finite, step-by-step, well-defined set of instructions used to solve a specific
problem or perform a task.
Characteristics of an Algorithm:
1. Input – Takes zero or more inputs.
2. Output – Produces at least one output.
3. Definiteness – Steps must be clear and unambiguous.
4. Finiteness – Must terminate after a finite number of steps.
5. Effectiveness – Each step must be simple and executable.
Example of Algorithm (Largest of Two Numbers):
Step 1: Start
Step 2: Input two numbers A and B
Step 3: If A > B, display A
Step 4: Else display B
Step 5: Stop
Algorithm Design:
Algorithm design is the process of developing a logical and efficient solution to a problem before
writing code.
Steps in Designing an Algorithm:
1. Understand the problem clearly.
2. Identify inputs and expected outputs.
3. Choose an appropriate strategy.
4. Write pseudocode or draw a flowchart.
5. Analyze time and space complexity.
Common Algorithm Design Techniques:
1. Brute Force – Trying all possible solutions.
2. Divide and Conquer – Breaking problem into smaller sub-problems.
3. Greedy Method – Making the best choice at each step.
4. Dynamic Programming – Solving subproblems and storing results.
5. Recursion – Function calling itself.
Algorithm Implementation:
Implementation means converting the designed algorithm into a programming language.
Steps in Implementation:
1. Select a programming language.
2. Write the program code.
3. Compile and execute the program.
4. Debug errors if any.
5. Test the program with different inputs.
Advantages of Algorithms:
1. Provides clear problem-solving steps.
2. Improves program efficiency.
3. Language independent.
4. Easy to debug and maintain.
Time and Space Complexity:
Time Complexity measures the time required to run an algorithm.
Space Complexity measures the memory used by an algorithm.
Big-O Notation is commonly used to represent complexity (e.g., O(n), O(n^2)).
Conclusion:
An algorithm is the foundation of programming. Proper design and implementation ensure efficient
and reliable software development.