Determining Binary Search Running Time & Understanding

In summary, the conversation is about a confusion regarding an equation related to binary search. The equation in question is int midIndex = (endIndex - startIndex / 2) + startIndex, where endIndex and startIndex are given specific values. The person asking for help believes that the midIndex should be 5, but the other person points out that there may be a typo in the equation and suggests correcting it to (endIndex - startIndex ) / 2 + startIndex. The original poster then confirms that the typo was the source of their confusion.
  • #1
zak100
462
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
  • #2
zak100 said:
int midIndex = (endIndex - startIndex / 2) + startIndex;
You must have copied this incorrectly. It should be (endindex - startindex)/2 + startindex
 
  • #3
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;
 

Related to Determining Binary Search Running Time & Understanding

1. How do you determine the running time of a binary search algorithm?

The running time of a binary search algorithm can be determined by counting the number of comparisons made during the search. Each iteration of the search involves dividing the search space in half, so the running time is proportional to the logarithm of the size of the search space.

2. What is the time complexity of a binary search algorithm?

The time complexity of a binary search algorithm is O(log n), where n is the size of the search space. This means that as the size of the input increases, the running time increases at a slower rate than the size of the input.

3. How does the input size affect the running time of a binary search algorithm?

The input size directly affects the running time of a binary search algorithm. As the input size increases, the running time increases at a slower rate. This is because the search space is divided in half with each iteration, making the algorithm more efficient for larger input sizes.

4. What is the best case scenario for a binary search algorithm?

The best case scenario for a binary search algorithm is when the target element is located at the middle of the search space. This means that the algorithm will only need to make one comparison before finding the target element, resulting in a time complexity of O(1).

5. How can understanding binary search running time be useful in real-world applications?

Understanding binary search running time can be useful in real-world applications where large datasets need to be searched quickly. By knowing the time complexity of the algorithm, developers can optimize their code to improve performance and make the search process more efficient. This can be particularly useful in tasks such as searching through large databases or finding specific elements in an array.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top