One useful algorithm

There are many good algorithms solving many problems. But there is one algorithm which solves almost everything. I used this algorithm way before I knew that there are any algorithms. This algorithm is called Binary Search and many of you may know it. But did you know that with combination of regression it can solve almost any integer equation or problem ? Sometimes you don’t need to understand things to know the answers. You don’t need to solve equation if you can approximate solution and guess it on third try. Yes of course you need to at least think of the function and its graph but usually it is simple function with simple solution. I use this method in many things that appear to be harder to understand than to give a solution. I pick a number which represents the solution I try to use it in equations and I see something. When I try other number I see different result. I slowly get feeling of how this function is running and I use Binary Search algorithm to solve it. This is how Binary search works:

Pick a two numbers, One you think is the lowest value and one you think is the highest possible value. Then simply pick middle value and try it, if you can see the real solution is higher just pick middle value of the highest value and the previous middle value. If you see that real solution is lower pick middle value of the lowest value and the previous middle value. If you do this recursively you will quickly get a solution in log(n) time. Simplified algorithm:

Max = mid
Mid = (mid+min)/2
} else
Min = mid
Mid = (max+mid)/2

Leave a Comment

Your email address will not be published. Required fields are marked *