Skip to main contentSkip to solution

If the list to be searched using binary search contains an even number of elements, then the mid-value is calculated using _______________.

Solution

✅ Correct Option: 1

With an even number of elements there are two middle elements, so low+high2\dfrac{low + high}{2} is not a whole number. Floor division (// in Python) rounds it down to a valid index, so the lower of the two middle elements is taken.

For 6 elements with indices 0 to 5: ⌊0+52⌋=⌊2.5⌋=2\left\lfloor \dfrac{0 + 5}{2} \right\rfloor = \lfloor 2.5 \rfloor = \boxed{2}


Options 3 and 4: the average or mean of the two indices gives a decimal such as 2.5, which cannot be used as a list index.

Option 2: ceil division would also give a whole number, but the standard rule is to use floor division.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question