Bifalgorithm post
What if I told you that you can find one number in a list of 1 million elements in just about 20 steps. That's the power of binary search. Binary search is one of the fastest searching algorithm, but there is one important condition that ray must be sorted. Suppose the rays having 10, 20, 30, 40, 50, 60 and 70 and you want to find 50 first check the middle element 40 since 50 is greater than 40 ignore the left half now search only the right half again check the middle of the element you will find 50 immediately instead of checking every element binary search keeps cutting the search space in half. That's why its time complexity is big of log n. Even with 1 million sorted elements, it takes only about 20 comparisons to find the target. Remember this interview tip, no sorted array, no binary search. Follow interview prep with SANA for more DSA concepts in 60 seconds.
What if I told you that you can find one number in a list of 1 million elements in just about 20 steps. That's the power of binary search. Binary search is one…
Ideas in this flowchart
- Binary Search
- Fast Searching Algorithm
- Condition: Array Must Be Sorted
- Example
- Array: 10, 20, 30, 40, 50, 60, 70
- Find 50
- Check Middle Element: 40
- 50 > 40, Ignore Left Half
- Search Right Half
- Find 50 Immediately
- Time Complexity: O(log n)
- 1 Million Elements in 20 Comparisons
- Interview Tip
- No Sorted Array, No Binary Search
- Follow Interview Prep with SANA