Treat the live array segment as a FIFO queue of runs. Remove its first two runs, append their merged result, and repeat:
PROCEDURE Sort(a, b: INTEGER; VAR c, d: INTEGER);
VAR x, y, z: INTEGER;
BEGIN
  c := a; d := b;
  WHILE c < d DO
    x := Run(c, d);
    IF x = d THEN RETURN END;
    y := Run(x, d);
    z := y-c;
    Merge(c, x, y, d);
    c := y;
    d := d+z
  END
END Sort;
Compute both run boundaries before writing the destination, which starts at the old d. The untouched suffix is followed by the new merged run. The endpoints returned in c,d always describe the live sequence; obsolete values before c are ignored. As allowed, assume enough destination capacity.
For example, becomes the live sequence , then . The first merge combines and , and the second combines and .

Articles by others on the same topic (0)

There are currently no matching articles.