Algorithms and Computational Thinking
Designing algorithms
What an algorithm is
An algorithm is a finite set of steps for solving a problem. Good algorithms are correct, efficient, and understandable. Computational thinking involves decomposing problems, recognizing patterns, abstracting important details, and designing step-by-step solutions.
Correctness comes first
An algorithm that is very fast but gives the wrong answer is not useful. Prove or test correctness before optimizing.
Ways to evaluate algorithms
Correctness
- Does it always produce the right answer?
- Can it be proven or tested?
Time efficiency
- How does runtime grow as input grows?
- Often described with asymptotic notation
Big-O notation
Big-O notation describes growth in resource usage. For example, an algorithm with runtime typically grows proportionally with input size, while grows much faster. Big-O ignores constant factors and lower-order terms to focus on scaling behavior.
Which runtime usually scales better for large inputs?
grows more slowly than , , or for large .
Correct answer:
Why is Big-O notation useful?
It focuses on asymptotic growth rather than machine-specific details.
Correct answer: It helps compare how algorithms scale as input size grows.
Algorithm design patterns
Common strategies include brute force, divide and conquer, greedy choice, dynamic programming, and backtracking. Each works well for certain problem structures, so choosing the right one is a major part of problem solving.
Which strategy typically solves a problem by combining solutions to smaller subproblems?
Divide and conquer breaks a problem into smaller pieces and merges the results.
Correct answer: Divide and conquer
Name one situation where a greedy algorithm might be appropriate.
Greedy methods work only when the problem has the right structure.
Correct answer: When locally optimal choices lead to a globally optimal solution, such as interval scheduling.