2 2 votes How many comparisons are needed for a binary search in a set of 64 elements? Algorithms algorithms binary-search numerical-answers + – Rohan Mundhey 7.0k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
1 1 vote in quetion not mentaioned that Array is sorted or not : I assume it is sorted . it will take log2n for n elements. So for 64 it takes 6 comparision atmost for any element. Comparision on elements 32, 16, 8, 4, 2,1 Prashant. answered Nov 9, 2016 • edited Nov 9, 2016 by Prashant. Prashant. comment Share Follow See all 19 Comments 19 19 Comments reply Kapil commented Nov 9, 2016 reply Follow flag That is the time complexity approximation, right ? It does not gives the number of comparisons 0 0 replyShare Prashant. commented Nov 9, 2016 reply Follow flag Kapil in worst case we need to one side from half of array na. so logn max comparision in worst case ? M i missing somthing? 0 0 replyShare Kapil commented Nov 9, 2016 reply Follow flag Its a formula type of a question, I dont remember the formula, but somewhere i read that in the worst case, it uses 2 * Log2(n + 1) maximum comparisons and its asymptotic upper bound is O(Log n). 0 0 replyShare Pavan Kumar Munnam commented Nov 9, 2016 reply Follow flag this is taken from this site https://www.topcoder.com/community/data-science/data-science-tutorials/binary-search/ 0 0 replyShare Kapil commented Nov 9, 2016 reply Follow flag @Pavan, O(Log N) is the asymptotical number of comparisons needed. But it is different from the actual number of worst case comparisons. I guess WC comparisons are 2 * Log2(n + 1) = 14 1 1 replyShare Rohan Mundhey commented Nov 9, 2016 reply Follow flag from where did you got this formula...can you explain it ? 0 0 replyShare air1 commented Nov 9, 2016 reply Follow flag I tried it for a few cases by hand and I could only get maximum comparisons ceil(log(N)). How do you get 2*logN +1 ? 0 0 replyShare air1 commented Nov 9, 2016 reply Follow flag Formula given by kapil is correct. Cehck this http://stackoverflow.com/questions/10571170/how-many-comparisons-will-binary-search-make-in-the-worst-case-using-this-algori 1 1 replyShare Rohan Mundhey commented Nov 9, 2016 reply Follow flag formula is 2*Log(N-1) = 2*ceil[log(63)] = 2*6 Ans :12 0 0 replyShare Prashant. commented Nov 9, 2016 reply Follow flag formula given is 2*Log(N-1) in link not 2*Log(N +1) 1 1 replyShare Pavan Kumar Munnam commented Nov 9, 2016 reply Follow flag i think it depends on the binary search implementation... 0 0 replyShare Kapil commented Nov 9, 2016 reply Follow flag @Anirudh, See the 2nd answer in the link !! He has given exact upper bound 0 0 replyShare Prashant. commented Nov 9, 2016 reply Follow flag I will check it b/c in low answer says exact 2*Log(N-1) BDW thankyou for pointing. 1 1 replyShare Arjun commented Nov 9, 2016 reply Follow flag Isn't that stackoverflow link saying for that given implementation? Why do we increase the formula? :O 0 0 replyShare air1 commented Nov 9, 2016 reply Follow flag @Arjun Sir please check the discussions under the second answer here https://gateoverflow.in/16831/average-number-comparisons-binary-search-consecutive-starting According to the number of comparisons will be logN. So I think it depends on what we count as a comparison or if all the information is available in a single compare operation. 0 0 replyShare Kapil commented Nov 9, 2016 reply Follow flag @Arjun Sir, But O(Log N) is the asymptotic number of comparisons. Are we going to use this ? I guess No. 0 0 replyShare Arjun commented Nov 9, 2016 reply Follow flag yes, we do not have more information here. At least choice could have helped. 1 1 replyShare Kapil commented Nov 9, 2016 reply Follow flag A). 6 B). 14 C). 12 D). None of the above :) :) :) 0 0 replyShare air1 commented Nov 9, 2016 reply Follow flag @kapil If we consider that one compare operation gives us all the information (wether the element being searched is to be searched on the left part or right or already found), then logN does represent the number of compare operations in the worst case for an array of $2^N$ elements. 1, 2, 3, 4, 5, 6, 7, 8 suppose we are searching for 0. first comparison with 4. now we know we have to search on left portion. so compare with 2, then with 1. total 3 comparisons. $2^3$ = $8$ 1 1 replyShare Please log in or register to add a comment.
1 1 vote we know that recurrance relation for binary search with n elements is: f(n) = f(n/2) + 2 now, f(64) = f(32) + 2 f(64) = (f(16) + 2) +2 f(64) = (f(8) + 4) +2 f(64) = (f(4) + 6) +2 f(64) = (f(2) + 8) +2 f(64) = (f(1) + 10) +2 now, f(1) equals to 2 since two comparisons are required for one number this gives f(64) = 14 Nirmal Gaur answered Apr 18, 2017 Nirmal Gaur comment Share Follow 0 reply Please log in or register to add a comment.