Bifalgorithm post
Some of you had doubts about why the time complexity of binary search is log n. Let's try to look at it mathematically. When we start binary search, the length of the array that we're dealing with is n, because there are n terms. And in the first iteration, when we divide the array into half, the length of the array becomes n by 2. And in the next iteration, when we again divide into half, the length of the array becomes n by 2 square, which is n by 4. In the next iteration, it becomes n by 2 cube and so on. So after the kth iteration, the length of the array becomes n by 2 square which is n by 4. In the next iteration it becomes n by 2 cube and so on. So after the kth iteration the length of the array that you are dealing with becomes n by 2 power k. Now let's say after k iterations the length of the array that you're dealing with is 1. So you have only one element to compare. Then n by 2 power k becomes equal to 1 and when you take log on both the sides the number of iterations k becomes equal to log n. And in the worst case this is what we keep doing in binary search, we keep searching on both the sides till we are comparing one last element. I hope this made sense, if you have any more doubts let me know. I am here to clear all the doubts.
Some of you had doubts about why the time complexity of binary search is log n. Let's try to look at it mathematically. When we start binary search, the length…
Ideas in this flowchart
- Time Complexity of Binary Search
- Initial Array Length
- Iteration Process
- First Iteration
- Second Iteration
- General Iteration
- Length After k Iterations
- Final Comparison
- Mathematical Conclusion
- Equation Setup
- Logarithmic Transformation
- Conclusion on k