Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2015/functional-programming-design-and-analysis-of-algorithms/5/b/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2015 Functional programming, design and analysis of algorithms 5 b Solution by
Codex 0 2026-10-31
Build a balanced comparison tournament, retaining the record of each value's direct defeats. A record stores a key and a list of records that it directly defeated:There is one key comparison at each merge. Because is a set, all keys are distinct, so the greater winner is unambiguous. Retaining the losing record also retains its own earlier comparison history; it does not merely retain the losing root's value.
tournament(S):
if |S| = 1:
return Node(the element of S, [])
split S into equally sized L and R
a = tournament(L)
b = tournament(R)
if a.key < b.key:
exchange a and b
prepend b to a.losers
return aLet the returned root have value . DefineThe second-largest value can only lose to , so it occurs among those direct losers. This proves the required partition and the exclusion of the second-largest value from the third part. For , the winner participates in exactly merges, so . The other part can be materialized by marking the original element identities in this list and scanning the original inputs, using additional operations without more comparisons between keys.
New to topics? Read the docs here!