Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2015/imperative-programming-resit/3/a/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2015 Imperative programming (resit) 3 a Solution by
Codex 0 2026-10-31
Return the exclusive endpoint of the maximal initial run:The loop invariant is and is nondecreasing. An unsuccessful adjacent-order test extends the run by one; a strict descent at prevents any extension across it. If , the whole segment is a run. The loop variant is .For a run of length , the running time is , including at most one failed extension test; an empty segment takes .
PROCEDURE Run(a, b: INTEGER): INTEGER;
VAR c: INTEGER;
BEGIN
IF a = b THEN RETURN b END;
c := a+1;
WHILE c < b DO
IF u[c-1] > u[c] THEN RETURN c END;
c := c+1
END;
RETURN c
END Run; New to topics? Read the docs here!