Computer science · University
Algorithms, invariants and asymptotic costs
An algorithm should have a stated input contract, output goal and termination argument. A loop invariant describes what remains true at a defined point in each iteration.
Prerequisites
Relevant school subject foundations and careful reasoning.
Study notes
Correctness
An algorithm should have a stated input contract, output goal and termination argument. A loop invariant describes what remains true at a defined point in each iteration.
Growth
Big-O describes asymptotic upper growth bounds, not exact seconds on every machine. State the operation and input size being counted.
Search
Binary search requires a sorted sequence and consistent ordering. It reduces the remaining interval at each step.
Testing
Edge cases such as empty inputs, duplicate values and boundaries complement proofs; passing examples alone does not prove correctness.
Worked examples
Which input condition does ordinary binary search require?
Ordering enables discarding half the interval safely.
A sorted sequence under the comparison rule
A procedure performs exactly 3n+2 operations in a stated model. Find the count for n=10.
3×10+2=32.
32
Check your understanding
What does a loop invariant describe?
- A condition preserved at a specified loop point
- The processor brand
- A random input guess
- Only the final printed word
An interval of 64 candidates is halved exactly three times. How many remain?
Why does O(n) not tell us an exact runtime in seconds?
Which is an important test input for a search function?
- Only a screenshot
- Only a large output font
- An empty collection
- Only one usual successful case
Answers and marking guidance
Answers and marking notes
- A condition preserved at a specified loop point
Initialization, preservation and termination connect the invariant to correctness.
- 8
64/2³=8.
- It describes asymptotic growth under a cost model, while constants, hardware, implementation and input details affect wall-clock time.
Credit growth versus measured runtime.
- An empty collection
Boundary cases can reveal indexing and termination bugs.
Further reading and review references
These are reference links, not copied exercises, institutional endorsement or proof of independent review.
AI-assisted preview; independent educational review required. Level labels describe prerequisites, not accreditation or official curriculum alignment.