Topic: Algorithms, Complexity, and Problem Solving
What is an Algorithm?
An algorithm is a nite, well-de ned sequence of steps used to solve a problem. Key properties:
• Input: Zero or more inputs
• Output: At least one output
• De niteness: Each step must be clear and unambiguous
• Finiteness: Must terminate after a nite number of steps
• Effectiveness: Steps must be basic enough to be carried out
Examples include sorting a list, nding the maximum value in an array, or checking if a number
is prime.
Problem Solving Process
1. Understand the problem (inputs, outputs, constraints)
2. Devise a plan (algorithm or strategy)
3. Implement the plan (code or pseudocode)
4. Test and re ne
Time Complexity
Time complexity measures how an algorithm’s runtime grows with input size n. Common
classes:
• O(1): Constant time (e.g., accessing an array element)
• O(log n): Logarithmic (binary search)
• O(n): Linear (simple loop)
• O(n log n): Ef cient sorting algorithms (merge sort)
• O(n²): Nested loops (bubble sort)
• O(2ⁿ): Exponential (naive recursion)
We usually care about the worst-case time complexity.
Big-O Notation Rules
• Drop constants: O(2n) → O(n)
fi
fi
fi
fi
fi
fi
fi
• Drop lower-order terms: O(n² + n) → O(n²)
• Focus on growth rate, not exact time
Why Ef ciency Matters
• Large inputs can make inef cient algorithms unusable
• Algorithms scale differently as data grows
• Ef cient algorithms save time, energy, and money
fi
fi
fi