Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2016/imperative-programming/4/c/i/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2016 Imperative programming 4 c i Solution by
Codex 0 2026-10-31
Write the segment as , where and . The desired array rotation is . Reverse each block, then the whole segment:The state changes are , using the list reversal identity supplied in the paper and reversal being an involution. Three reversals implement the right rotation in time and auxiliary space. The initial return covers empty and identity rotations.
PROCEDURE rotate(i, n, k: INTEGER);
BEGIN
IF (k = 0) OR (k = n) THEN RETURN END;
reverse(i, n-k);
reverse(i+n-k, k);
reverse(i, n)
END rotate; New to topics? Read the docs here!