0

In python binary search to find the mid value i use:

mid=math.ceil((high+low)/2).

Is ceil more optimal or should I use floor or round?

1 Answer 1

1

for the algorithm there is no difference in performance, i think the only difference is in the cost of the operation, floor should be the easiest one because the computer doesn't need to do anything except remove the floating point part.

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

2 Comments

In general [not specific to python] which one can i use, ceil or floor?
you can use both, just remember that in some case for example with low=0 and high=1 you can get stuck there forever so you have to check that the low and high are changed since the last cycle. there is no performance difference.

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.