What is an Algorithm?
What is an Algorithm?
Imagine you're making a peanut butter and jelly sandwich. You don't just randomly throw ingredients together—you follow a step-by-step process: get bread, spread peanut butter, spread jelly, put slices together. That's exactly what an algorithm is in programming!
An algorithm is a step-by-step set of instructions to solve a problem or complete a task.
Why Do We Need Algorithms?
Think about your daily routines:
- ›Morning routine: Wake up → Brush teeth → Shower → Get dressed → Eat breakfast
- ›GPS navigation: Find your location → Find destination → Calculate shortest path → Provide directions
- ›Google Search: Take your query → Search billions of pages → Rank results → Display best matches
Similarly, in programming, algorithms help us:
- ›Solve problems systematically - Break down complex problems into simple steps
- ›Write efficient code - Complete tasks faster and use less memory
- ›Make decisions - Choose the best approach for a problem
- ›Automate tasks - Let computers do repetitive work
💡 Simple Definition: An algorithm is like a recipe—it tells you exactly what steps to follow to get the result you want.
Real-World Analogy
Let's understand algorithms with everyday examples:
🍳 Cooking Recipe (Algorithm Example)
Problem: Make scrambled eggs
Algorithm:
Step 1: Crack 2 eggs into a bowl
Step 2: Add salt and pepper
Step 3: Beat eggs until mixed
Step 4: Heat pan with butter
Step 5: Pour eggs into pan
Step 6: Stir until cooked
Step 7: Serve on plate
This is an algorithm! It has:
- ›✅ Clear inputs (eggs, salt, pepper, butter)
- ›✅ Step-by-step instructions
- ›✅ Clear output (scrambled eggs)
🔍 Finding a Word in Dictionary (Search Algorithm)
Problem: Find the word "algorithm" in a dictionary
Algorithm 1 (Linear Search):
Step 1: Start from page 1
Step 2: Read each word
Step 3: If word matches, stop
Step 4: If not, go to next word
Step 5: Repeat until found
⏱️ Time: Very slow for large dictionaries!
Algorithm 2 (Binary Search):
Step 1: Open dictionary in the middle
Step 2: Is "algorithm" before or after this page?
Step 3: If before, search left half; if after, search right half
Step 4: Repeat until found
⚡ Time: Much faster!
This shows that different algorithms can solve the same problem, but some are more efficient than others!
Your First Algorithm: Find Maximum Number
Let's write our first algorithm to find the largest number in a list.
Problem Statement
Input: [25, 30, 22, 28, 35]
Output: 35 (the maximum number)
Algorithm (Step-by-Step)
Step 1: Assume first number is the maximum
Step 2: Go through each remaining number
Step 3: If current number > maximum, update maximum
Step 4: Repeat until all numbers are checked
Step 5: Return the maximum
Implementation
1# Find maximum number in a list
2numbers = [25, 30, 22, 28, 35]
3
4# Step 1: Assume first number is max
5max_num = numbers[0] # max_num = 25
6
7# Step 2-4: Check each number
8for num in numbers:
9 if num > max_num:
10 max_num = num
11
12# Step 5: Print result
13print(f"Maximum number: {max_num}") # Output: 35🎯 Trace the Algorithm: Try following this with [10, 5, 20, 15]. What's the maximum? Can you trace each step?
Characteristics of a Good Algorithm
A good algorithm must have these 5 properties:
1. Input - Clear Starting Point
The algorithm must clearly define what inputs it needs.
Example:
- ›❌ Bad: "Find the maximum"
- ›✅ Good: "Find the maximum number in a list of integers"
2. Output - Clear Result
The algorithm must produce a clear, well-defined output.
Example:
- ›❌ Bad: "Process the numbers"
- ›✅ Good: "Return the maximum number"
3. Definiteness - Unambiguous Steps
Each step must be clear and not confusing.
Example:
- ›❌ Bad: "Pick some numbers and do something"
- ›✅ Good: "Compare current number with maximum. If greater, update maximum."
4. Finiteness - Must Terminate
The algorithm must end after a finite number of steps.
Example:
- ›❌ Bad (Infinite Loop):
while True:
print("Hello") # Never stops!
- ›✅ Good (Finite):
for i in range(5):
print("Hello") # Stops after 5 times
5. Effectiveness - Achievable Steps
Each step must be basic enough to be carried out.
Example:
- ›❌ Bad: "Use magic to find the answer"
- ›✅ Good: "Compare two numbers using < operator"
Types of Algorithms
Different problems need different types of algorithms:
| Algorithm Type | Purpose | Example |
|---|---|---|
| Searching | Find an item in a collection | Find "Alice" in a list of names |
| Sorting | Arrange items in order | Sort [5, 2, 8, 1] → [1, 2, 5, 8] |
| Recursion | Solve by breaking into smaller problems | Calculate factorial: 5! = 5 × 4 × 3 × 2 × 1 |
| Divide & Conquer | Break problem in half repeatedly | Binary search in dictionary |
| Greedy | Make best choice at each step | Making change with coins |
| Dynamic Programming | Store results to avoid recomputation | Fibonacci sequence |
Algorithm Example: Sum of Array
Let's write another algorithm to find the sum of all numbers in an array.
Problem
Input: [10, 20, 30, 40, 50]
Output: 150 (sum of all numbers)
Algorithm Steps
Step 1: Initialize sum = 0
Step 2: For each number in the array:
Add number to sum
Step 3: Return sum
Implementation
1# Calculate sum of array
2numbers = [10, 20, 30, 40, 50]
3
4# Step 1: Initialize sum
5total = 0
6
7# Step 2: Add each number
8for num in numbers:
9 total += num
10 print(f"Current sum: {total}")
11
12# Step 3: Print final sum
13print(f"Total sum: {total}") # Output: 150Algorithm vs Program
Many people confuse algorithms with programs. Here's the difference:
| Algorithm | Program |
|---|---|
| Step-by-step logic | Actual code implementation |
| Written in plain language or pseudocode | Written in a programming language |
| Independent of language | Depends on specific language (Python, Java, C++) |
| Design phase | Implementation phase |
| What to do | How to do it in code |
Example:
Algorithm (Language-Independent):
1. Take two numbers as input
2. Add them together
3. Display the result
Program (Language-Specific):
# Python implementation
num1 = 5
num2 = 10
result = num1 + num2
print(result)
// Java implementation
int num1 = 5;
int num2 = 10;
int result = num1 + num2;
System.out.println(result);
// C++ implementation
int num1 = 5;
int num2 = 10;
int result = num1 + num2;
cout << result;
How to Design an Algorithm
Follow these steps to design any algorithm:
Step 1: Understand the Problem
- ›What is the input?
- ›What is the output?
- ›What are the constraints?
Step 2: Break Down the Problem
- ›Divide into smaller, manageable steps
- ›Think about what you would do manually
Step 3: Write the Steps
- ›Write in plain English or pseudocode
- ›Make each step clear and simple
Step 4: Trace with Examples
- ›Test with sample inputs
- ›Check if you get correct output
Step 5: Optimize
- ›Can you make it faster?
- ›Can you use less memory?
Practice Problem: Count Even Numbers
Let's design an algorithm together!
Problem: Count how many even numbers are in an array.
Example:
- ›Input: [1, 2, 3, 4, 5, 6]
- ›Output: 3 (even numbers are 2, 4, 6)
Your Turn: Design the Algorithm
Think about these questions:
- ›How do you check if a number is even?
- ›How do you count occurrences?
- ›What steps would you follow?
Solution
1# Count even numbers in array
2numbers = [1, 2, 3, 4, 5, 6]
3
4# Step 1: Initialize counter
5even_count = 0
6
7# Step 2: Check each number
8for num in numbers:
9 # Step 3: If even, increment counter
10 if num % 2 == 0:
11 even_count += 1
12 print(f"{num} is even")
13
14# Step 4: Display result
15print(f"Total even numbers: {even_count}") # Output: 3Why Study Algorithms?
1. 🧠 Develop Problem-Solving Skills
Algorithms teach you to think logically and break problems into steps.
2. ⚡ Write Efficient Code
Good algorithms make your programs faster and use less memory.
3. 💼 Ace Technical Interviews
Every coding interview asks algorithm questions:
- ›"How would you find duplicates in an array?"
- ›"Design an algorithm to sort a million numbers"
- ›"What's the most efficient way to search?"
4. 🚀 Build Better Applications
Real applications use algorithms:
- ›Google Search - Ranking algorithm
- ›Netflix - Recommendation algorithm
- ›Waze - Shortest path algorithm
- ›Instagram - Feed ranking algorithm
5. 📚 Foundation for Advanced Topics
Algorithms are essential for:
- ›Machine Learning
- ›Artificial Intelligence
- ›Database Optimization
- ›Network Routing
- ›Cryptography
Key Takeaways
✅ Algorithm = Step-by-step instructions to solve a problem
✅ 5 Properties: Input, Output, Definiteness, Finiteness, Effectiveness
✅ Algorithm ≠ Program: Algorithm is logic, Program is implementation
✅ Different algorithms can solve the same problem with different efficiency
✅ Practice makes perfect: The more algorithms you design, the better you become!
What's Next?
Now that you understand algorithms, let's dive deeper:
- ›Time Complexity - How to measure algorithm efficiency
- ›Space Complexity - How much memory algorithms use
- ›Searching Algorithms - Learn different ways to find data
Practice Exercises
Try these algorithm design challenges:
Exercise 1: Find Minimum Number
Design an algorithm to find the smallest number in an array.
Input: [45, 23, 67, 12, 89]
Output: 12
1# Your code here
2numbers = [45, 23, 67, 12, 89]
3
4# TODO: Write algorithm to find minimumExercise 2: Count Positive Numbers
Design an algorithm to count how many positive numbers are in an array.
Input: [-5, 10, -3, 8, 0, -1, 6]
Output: 3 (positive numbers: 10, 8, 6)
1# Your code here
2numbers = [-5, 10, -3, 8, 0, -1, 6]
3
4# TODO: Count positive numbersReady to learn more? Let's explore Time Complexity (Big-O) next! 🚀