In python, while implementing the binary search algorithm, which math function is optimal to be used to find out the mid value - floor or ceil ?
You don't need to use either ceil or floor function for implementing binary search in python. Depending on the problem, you have to round the mid value up or down.
mid = low + (high-low)/2 #rounds down the mid value
mid = low + (high-low+1)/2 #rounds up the mid value
Try to solve these two problems, you will get an idea how this works.
First try these problems on your own and if you get stuck, refer to the this.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments