Determining Binary Search Running Time & Understanding

Click For Summary
The discussion centers on a misunderstanding of the equation used to calculate the midIndex in a binary search algorithm. The original equation provided was incorrect, leading to an erroneous calculation of midIndex. The correct formula is (endIndex - startIndex) / 2 + startIndex, which accurately computes the midpoint. The user initially believed the midIndex should be 5 but miscalculated it as 10 due to the error in the equation. The clarification resolved the confusion regarding the proper calculation method.
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;
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
Replies
2
Views
3K
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K