Determining Binary Search Running Time & Understanding

Click For Summary

Discussion Overview

The discussion revolves around understanding the correct equation for calculating the midIndex in a binary search algorithm. Participants are examining a specific equation and its implications for determining the midIndex value.

Discussion Character

  • Homework-related

Main Points Raised

  • One participant presents an equation for calculating midIndex but expresses confusion about its correctness, suggesting that the result does not align with expectations.
  • Another participant points out a potential error in the original equation, proposing a corrected version that includes proper parentheses to ensure correct order of operations.
  • A third participant questions the source of the original equation and suggests it may simply be a typo, reiterating the corrected form.
  • The original poster acknowledges the feedback and confirms the source of the equation, indicating that the confusion has been resolved.

Areas of Agreement / Disagreement

Participants generally agree on the corrected form of the equation, but there is no consensus on the original equation's validity prior to the correction.

Contextual Notes

The discussion highlights the importance of proper notation and parentheses in mathematical expressions, particularly in programming contexts, but does not resolve any broader implications of the binary search algorithm itself.

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
3K
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 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K