All lessons in Software Engineering: Programs, Systems, and Maintainable Software
Sorting, Binary Search, and Boundary Proofs
Implement lower-bound search and justify sorting choices with invariants and edge cases.
40 minutes · Free with an account · preview
What you will learn
- State and maintain a lower-bound binary-search invariant.
- Distinguish stable sorting, comparison keys, and deterministic tie-breaking.
- Test empty, duplicate, and boundary inputs systematically.
An overdue report is sorted by due date. You need the first loan whose due date is not earlier than a chosen date. Returning an arbitrary matching loan is insufficient when several share that date. The required operation is a boundary search, and its contract should say which boundary it returns.
Many search defects are off-by-one errors caused by mixing interval conventions. Choose a convention, name what lies outside the search interval, and update only the bound justified by the comparison.