Prev: W1, Next: W3

Zoom: Link, TopHat: Link (936525), GoogleForm: Link, Piazza: Link, Feedback: Link, GitHub: Link, Sec1&2: Link


Slide:

# Slides and Notes

📗 From sections 1 and 2:
➩ Complexity slides: Link.
➩ Timing notes: Link.
➩ Complexity notes: Link.
➩ List notes: Link.

# Performance vs Complexity

📗 Many things affect performance measured in total run time:
➩ (Not relevant) Speed of computer, CPU.
➩ (Not relevant) Speed of Python interpretor.
➩ (Complexity) Algorithms: strategy for solving the problem.
➩ (Complexity) Input sizes: size of input data.

# Step

📗 A step is a unit of work with bounded execution time.
📗 Complexity is the total number of steps as a function of input size.

# Big O Notation

📗 "O" for "order of growth" to categorize how fast functions grow.
➩ Do not care about scale.
➩ Do not care about small inputs.
➩ Care about the shape of the curve.
📗 Multiple of a general function that is an upper bound:
➩ If \(f\left(N\right) \leq C \cdot g\left(N\right)\) for large \(N\) values and some fixed constant \(C\);
➩ Then \(f\left(N\right) \in O\left(g\left(N\right)\right)\).
📗 Example: \(3 N^{5} + N^{4} \in O\left(N^{5}\right)\).
📗 Common classes: \(O\left(1\right) \subset O\left(\log N\right) \subset O\left(N\right) \subset O\left(N \log N\right) \subset O\left(N^{2}\right) \subset O\left(2^{N}\right) \subset O\left(N!\right)\).

# List vs Set Example

📗 Compare search in list and set (and tuple).
➩ Search in list (and tuple) is \(O\left(N\right)\).
➩ Search in sorted list (binary search) is \(O\left(\log\left(N\right)\right)\).
➩ Search in set (hashset) is \(O\left(1\right)\).

# Binary Search

📗 The list needs to be sorted (\(O\left(N \log\left(N\right)\right)\) to quick sort a list, one time cost at the beginning).
📗 Search: \(O\left(\log\left(N\right)\right)\) (log is base e, same class as \(O\left(\log_{2}\left(N\right)\right)\)).
➩ Find item in the middle: if smaller than the key, search left half; if larger than the key, search right half.
bisect in Python: Doc.
📗 Visualization: Link.

# Hash Set

📗 Each element \(x\) is stored at the address (on memory) \(h\left(x\right)\), where \(h\) is the called the hash function.
➩ For example \(h\left(x\right) = x \mod 100\) or \(h\left(x\right) = \left \lfloor{\sin\left(x\right) \cdot 100}\right \rfloor\). (\(\left \lfloor{...}\right \rfloor\) is the floor function).
📗 Collisions (multiple items want to be stored at the same address) can be handled by open addressing (probing) or chaining.
➩ Open addressing: probe the memory to find the next open address.
➩ Chaining: a list of items stored at every address.
📗 Search: \(O\left(1\right)\).
➩ Apply the hash function. If the key is not found, probe or search in the chain.
📗 Visualization: Link, Link


# Questions?



📗 Notes and code adapted from the course taught by Professors Gurmail Singh, Yiyin Shen, Tyler Caraza-Harter.
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google form Link at the end of the lecture.
📗 Anonymous feedback can be submitted to: Form. Non-anonymous feedback and questions can be posted on Piazza: Link

Prev: W1, Next: W3





Last Updated: February 10, 2026 at 10:03 PM