4.2.2.2 Binary Search
4.2.2.2 Binary Search
(Difficulty Note: This is slightly more complex than Linear Search, as it requires sorted data.)
Binary Search is a much faster way to find something, but it has a special rule: the list of items must be sorted (like numbers from smallest to largest, or words alphabetically). It works by repeatedly dividing the list in half.
How it works:
- Find the middle item in the sorted list.
- Is the middle item the one you're looking for?
- If Yes, you found it! Stop.
- If your item is smaller than the middle item, you know your item must be in the first half of the list. Get rid of the second half.
- If your item is larger than the middle item, you know your item must be in the second half of the list. Get rid of the first half.
- Now you have a much smaller list. Repeat steps 1 and 2 with the new, smaller list until you find your item or the list becomes empty (meaning your item isn't there).
Example: Imagine finding a word in a dictionary. You don't start at 'A' and read every word. You open to the middle. If your word is earlier, you go to the middle of the first half, and so on.
Let's find 23
in the sorted list: [5, 9, 12, 16, 23, 30, 41, 50]
- Middle is
16
.23
is larger than16
. New list:[23, 30, 41, 50]
- Middle of new list is
30
.23
is smaller than30
. New list:[23]
- Middle of new list is
23
. (Yes!) – You found it!
Binary search is incredibly fast for large, sorted lists, like how search engines quickly find information in their vast sorted databases.
Bibliography:
- Binary Search. (n.d.). GeeksforGeeks. Retrieved July 11, 2025, from https://www.geeksforgeeks.org/binary-search/
- What is Binary Search? (n.d.). Programiz. Retrieved July 11, 2025, from https://www.programiz.com/dsa/binary-search
Further Reading:
- Binary Search - A collection of 14 posts. (n.d.). freeCodeCamp.org. Retrieved from https://www.freecodecamp.org/news/tag/binary-search/