5

Here is an array with exactly 15 elements:

1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

Suppose that we are doing a binary search for an element. Indicate any elements that will be found by examining two or fewer numbers from the array.

What I've got: as we are doing binary search, so the number found by only one comparison will be 7th element = 7. For two comparison, this leads to second division of array. That is, number found can be either 3 or 11.

Am I right or not?

4
  • Sounds correct to me, might want to add in the assumption that when splitting an even number array in half you are using the smaller of the 2 possible numbers to split on. Commented Jul 29, 2013 at 16:25
  • yes, this lead you for two comparison, but sometimes array won't be ordered, you may want to order it before. Commented Jul 29, 2013 at 16:27
  • 1
    Given the array in the problem description, it would actually be 4 8 12. Commented Jul 29, 2013 at 16:30
  • arr[7] = 8, which may be leading to your confusion. You said the 7th element, you might mean the element at 7. Commented Jul 29, 2013 at 16:40

2 Answers 2

2

You are almost right, the first number is not seven but eight.

The others 2 will then be 4 and 12.

The correct answer would be 4, 8, 12

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks everyone for your quick reply
0

`I found the answer to be 8 that is the 7th element, the other elements found were 3.5th and 10.5th element of the sorted array. So, the next two numbers delved are 4 and 11.

explanation on how i got the answers.
given array is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

head=1
tail=15
middle= 0+14/2=7th element **0 is the index value of 1 and 14 is of 15**    
middle value turns to be 8 as it is the 7th element.

solving value for first half
head=1
tail=8
middle= 0+7/2=3.5 or 3rd element **0 is the index value of 1 and 7 is of 8**

middle value now turns to be 4 as it is the 3rd element.

solving value for second half
head=8
tail=15
middle= 7+14/2=10.5 or 10th element  **7 is the index value of 8 and 14 is 
of 15**

middle value now turns to be 11 as it is the 10th element of the array`

Blockquote

1 Comment

An explanation on how you get your answer, would be nice for ohter users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.