recategorized by
944 views
3 votes
3 votes
Consider a sorted array A of n integer elements, A[0]...A[n − 1].

A search operation is to be performed on this array using .Binary search algorithm. If the element being searched is in fact the last element of the array, what is the difference between the index of element being searched and the index of the mid element computed in the third round of search algorithm? (Ignoring the ceil and floor functions for indices)

$(A)\frac{7n-7}{8}$

$(B)\frac{5n-6}{6}$

$(C)\frac{n-1}{8}$

$(D)\frac{3n-3}{4}$

My analysis

Before first iteration $i=0,j=n-1$ and element being searched has index=$n-1$

During First iteration

$mid=\frac{0+(n-1)}{2}= \frac{n-1}{2}$, since $a[mid] \lt search\,element$, so

After first iteration

$i=\frac{n-1}{2}+1,j=n-1$

During second iteration $mid=\frac{\frac{n-1}{2}+1+n-1}{2}=\frac{3n-1}{4}$

Since $a[mid] \lt x$

So after second iteration

$i=\frac{3n-1}{4}+1,j=n-1$

During third iteration

$mid=\frac{\frac{3n-1}{4}+1+n-1}{2}=\frac{7n-1}{8}$

So,difference between the index of element being searched and the index of the mid element computed in the third round of search algorithm

$(n-1)\,- \frac{7n-1}{8}=\frac{n-7}{8}$

This is my answer.But it matches none of the options. Where I went wrong?
recategorized by

Please log in or register to answer this question.

Related questions

3 votes
3 votes
2 answers
1
rahul sharma 5 asked Mar 8, 2018
2,396 views
Why to prefer binary search over ternary search?Can someone give recreance relation for ternary search,so that i can compare both
4 votes
4 votes
1 answer
2
Aghori asked Nov 5, 2017
1,205 views
There are two sorted list each of length $n$. An element to be searched in the both the lists. The lists are mutually exclusive. The maximum number of comparisons require...