Solution (source code)

= Solution

Assume every indicated segment lies within <array bounds>, and lengths are nonnegative. Use a temporary value of element type `T`:
``
PROCEDURE swap(i, j: INTEGER);
VAR x: T;
BEGIN
  x := m[i]; m[i] := m[j]; m[j] := x
END swap;

PROCEDURE blockswap(i, j, n: INTEGER);
VAR t: INTEGER;
BEGIN
  t := 0;
  WHILE t < n DO
    swap(i+t, j+t);
    t := t + 1
  END
END blockswap;
``
The first procedure even works for $i=j$. For the second, disjointness ensures that each <swap> exchanges the two original entries at offset `t` without disturbing any other pair. Its <loop invariant> says that offsets below `t` have been exchanged, offsets from `t` through $n-1$ retain their original values, and everything outside the two segments is unchanged. The <loop variant> $n-t$ proves <algorithm termination>. \b[A block exchange uses $n$ swaps and $O(1)$ auxiliary space.] The stray `wp` printed after this source subpart is not an additional requirement.