+1 vote
in Class 12 by kratos

Apply binary search for the following sequence of number. 10,20,30,35,40,45,50,55,60 search for item 35.

1 Answer

+6 votes
by kratos
 
Best answer

Low = 0, High = 8 search_ele = 35
Mid_ele_idx = (low + high)/2
= (0+8)/2
= 4
Is 40 = 35? No
The list now is 10 20 30 35
Low =0, high= 3
Mid_ele_idx = (low + high) /2
= (0+3)/2
= 1
Is 20 = 35? No
The list now is 30 35
Low = 2, high = 3
Mid_ele_idx = (low + high)/2
= (2+3)/2
= 2
Is 30 = 35 No
The list now is 35
Low = 3, high = 3
Mid_ele_idx = (low + high) fl
= (3+3)/2

= 3
Is 35 = 35 yes
location = 3

Output:
The search element 35 is found at location 3.

...