Splitting a Half-Open Interval Recursively

Two half-open subintervals can cover a segment exactly once and give clear stopping conditions for recursive processing.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Name each subinterval
  2. Check the one-element interval
  3. Work through the first uneven split on paper
  4. Handle one position before calculating child calls

Name each subinterval

For \([2,7)\) with midpoint 4, the pieces are \([2,4)\) and \([4,7)\). The first ends just before four and the second includes four, so their union covers every original index once. This follows slice boundaries.

Check the one-element interval

In \([2,3)\), a floor midpoint can equal the start. Recursing on the unchanged right interval would stall. Stop when the segment length is zero or one, according to the task. See making recursive inputs smaller.[1]

Work through the first uneven split on paper

For a nonempty half-open interval \([a,b)\), choose \(m=a+\lfloor(b-a)/2\rfloor\). When the length is at least two, both resulting lengths are positive and smaller. The two intervals touch at \(m\) without overlapping. See sequence slice boundaries.

[2,7) → [2,4) and [4,7)
[2,4) → [2,3) and [3,4)
[4,7) → [4,5) and [5,7)
[5,7) → [5,6) and [6,7)
Leaves cover positions 2, 3, 4, 5, 6

Handle one position before calculating child calls

Splitting length one with floor division leaves one child equal to the original interval. Entering that child is infinite recursion in the mathematical model. Check the length before splitting and define what happens for length zero. Tests should compare every leaf position against the original input extent.

References

  1. Jeff Erickson, Algorithms ↩