retagged by
1,256 views

3 Answers

6 votes
6 votes
Both are wrong. It is $T(n) = T(n/2) + \theta(1)$

What is 1 or 2 there? It's unit? Actually that is computational cost of finding mid in binary search which you can't write as 1 or 2. You should use $\theta(1)$ to denote it is equal to any constant not just 1 or 2.  Set your equation accordingly.
1 votes
1 votes

Recurrence relation: The time to search in an array of N elements is equal to the time to search in an array of N/2 elements plus 1 comparison.

T(N) = T(N/2) + 1

Next we perform telescoping: re-writing the recurrence relation for N/2, N/4, …, 2

T(N) = T(N/2) + 1

T(N/2) = T(N/4) + 1

T(N/4) = T(N/8) + 1

            ……

            T(4) = T(2) + 1

T(2) = T(1) + 1

Next we sum up the left and the right sides of the equations above:

T(N) + T(N/2) + T(N/4) + T(N/8) + … +T(2) =

T(N/2) + T(N/4) + T(N/8) + … +T(2) + T(1) + (1 + 1 + … + 1)

The number of 1’s on the right side is LogN

Finally, we cross the equal terms on the opposite sides and simplify the remaining sum on the right side:

T(N) =  T(1) + LogN

T(N) =  1 + LogN

Therefore the running time of binary search is:

T(N) = O(LogN)

0 votes
0 votes

Recurrence relation is T(n) = T(n/2) + 1, where T(n) is the time required for binary search in an array of size n.

T(n) = T( n 2k )+1+ ··· + 1 Page 2 Since T(1) = 1

 when n = 2k, T(n) = T(1) + k = 1 + log2(n).

Related questions

11 votes
11 votes
2 answers
2
Shubhanshu asked Jun 5, 2017
11,203 views
The average successful search time taken by binary search on a sorted array of $10$ items?$2.6$$2.7$$2.8$$2.9$Answer is $2.9$My doubt:- But when I am using $log_2n$ for $...