A visual introduction to big O notation
17 days ago
- #Big-O-notation
- #performance-optimization
- #algorithm-complexity
- Big O notation describes the performance of a function based on input size growth, not actual time.
- Four common categories of Big O notation are constant (O(1)), logarithmic (O(log n)), linear (O(n)), and quadratic (O(n^2)).
- Linear functions (O(n)) grow proportionally with input size, as seen in the sum function example.
- Constant functions (O(1)) have execution time that does not increase with input size.
- Bubble sort is an example of a quadratic (O(n^2)) algorithm due to nested loops over the input.
- Binary search is logarithmic (O(log n)) because it halves the search space with each step.
- Optimizing code involves choosing efficient data structures (e.g., Set for O(1) lookups) and avoiding pitfalls like nested O(n) operations.
- Caching intermediate results can improve average-case performance without changing worst-case complexity.