| | | | | |

Set Algorithm Complexity and Control Structure

  • n1 = size of range 1, n2 = size of range 2; n = n1 + n2, m = MIN(n1,n2)
  • Unsorted input ranges - For each element in range 1, iterate through range 2:
    Runtime = Θ(n1 x n2)
  • Sorted input ranges - Iterate through each range one time only:
    Runtime = Θ(n1 + n2) = Θ(n)
  • Sorted range control structure
    • compare current elements from each input range
    • perform action based on comparison
    • increment past elements used for action
    • continue until an input range is exhausted
    • deal with tail of remaining range
    • runtime O(n)
  • Some algorithms (union, merge) must iterate through tail, resulting in runtime Θ (n)
  • Others (intersection, difference) may deal with tail by ignoring it, resulting in data-dependent "early bailout" and runtime Ω (m)

| | Top of Page | 12. Generic Set Algorithms - 3 of 7