Determining Binary Search Running Time & Understanding

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 1K views
zak100
Messages
462
Reaction score
11

Homework Statement


Hi,

I have a problem understanding the following equation related to binary search:

Homework Equations


int midIndex = (endIndex - startIndex / 2) + startIndex;

The Attempt at a Solution


If: endIndex = 10
startIndex = 0

then midIndex = (10 - 0/2) + 0;
midIndex = (10-0) +0;
midIndex = 10;
I don't think, this is right. midIndex should be 5 as name implies.

Some body please guide, what's my error.

Zulfi.
 
Physics news on Phys.org
zak100 said:
int midIndex = (endIndex - startIndex / 2) + startIndex;
You must have copied this incorrectly. It should be (endindex - startindex)/2 + startindex
 
zak100 said:
the following equation
Where did you find that equation ?
Is it as simple as a typo and what you want is int midIndex = (endIndex - startIndex ) / 2 + startIndex;