Bifalgorithm post
15 numbers, sorted. Find 29, fast. Check them one by one. 15 checks. Watch this instead. Two flags mark where the answer can live. Check the middle. 23. 23 is under 29. So everything on the left is out. Eight cells, gone. One comparison, cut the world in half. Middle of what's left, 35. Over 29, so the right side goes. Opposite direction, same trick. Three cells remain. Middle, 29. Found. Three checks, That's the whole search. 15 checks or three. Oh of N or O of log N. Cut the problem in half every time.
15 numbers, sorted. Find 29, fast. Check them one by one. 15 checks. Watch this instead. Two flags mark where the answer can live. Check the middle. 23. 23 is…
Ideas in this flowchart
- Search Algorithm
- Linear Search
- Check One by One
- 15 Checks
- Binary Search
- Two Flags
- Check Middle
- Middle is 23
- 23 < 29
- Left Side Out
- Middle is 35
- 35 > 29
- Right Side Out
- Middle is 29
- Found 29
- Efficiency
- O(N) vs O(log N)