3.1 Binary Search Algorithm
Notes and Hacks for Bianary lesson
Popcorn Hack 1
Binary Place Value Table for 25
Binary Digit | 2⁷ | 2⁶ | 2⁵ | 2⁴ | 2³ | 2² | 2¹ | 2⁰ |
---|---|---|---|---|---|---|---|---|
Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Binary (25) | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
✅ Calculation:
1×16 + 1×8 + 0×4 + 0×2 + 1×1 = 25
Popcorn Hack 2
🍿 Popcorn Hack #2: Binary Blitz!
Decimal | Binary | How |
---|---|---|
10 | 1010 | 1×8 + 0×4 + 1×2 + 0×1 |
15 | 1111 | 1×8 + 1×4 + 1×2 + 1×1 |
17 | 10001 | 1×16 + 0×8 + 0×4 + 0×2 + 1×1 |
Popcorn Hack 3
🍿 Popcorn Hack #3: Half & Half!
Task: Search for the number 18 in the sorted list:
[3, 6, 9, 12, 15, 18, 21, 24]
✅ Binary Search Steps:
-
Initial list: [3, 6, 9, 12, 15, 18, 21, 24]
Low = 0, High = 7
Mid = (0 + 7) // 2 = 3 → list[3] = 12 -
Compare 12 to 18:
12 < 18 → Search right half -
New list segment: [15, 18, 21, 24]
Low = 4, High = 7
Mid = (4 + 7) // 2 = 5 → list[5] = 18 -
Compare 18 to 18:
✅ Match found at index 5
Final Answer:
Binary Search finds 18 at index 5
Homework hacks
🧠 PART A: Binary Breakdown
- Convert 42 to Binary Division Steps:
42 ÷ 2 = 21 R0
21 ÷ 2 = 10 R1
10 ÷ 2 = 5 R0
5 ÷ 2 = 2 R1
2 ÷ 2 = 1 R0
1 ÷ 2 = 0 R1
Binary: 101010
Place Value Breakdown: 1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42
- Convert 19 to Binary Division Steps:
19 ÷ 2 = 9 R1
9 ÷ 2 = 4 R1
4 ÷ 2 = 2 R0
2 ÷ 2 = 1 R0
1 ÷ 2 = 0 R1
Binary: 10011
Place Value Breakdown: 1×16 + 0×8 + 0×4 + 1×2 + 1×1 = 19
- Convert 100 to Binary
Division Steps:
100 ÷ 2 = 50 R0
50 ÷ 2 = 25 R0
25 ÷ 2 = 12 R1
12 ÷ 2 = 6 R0
6 ÷ 2 = 3 R0
3 ÷ 2 = 1 R1
1 ÷ 2 = 0 R1
Binary: 1100100
Place Value Breakdown: 1×64 + 1×32 + 0×16 + 0×8 + 1×4 + 0×2 + 0×1 = 100
💡 PART B: Binary to Decimal Sprint
-
Binary: 101010 Place values: 1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42
-
Binary: 10011 Place values: 1×16 + 0×8 + 0×4 + 1×2 + 1×1 = 19
-
Binary: 110011 Place values: 1×32 + 1×16 + 0×8 + 0×4 + 1×2 + 1×1 = 51
🔎 PART C: Binary Search in Action
List: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33]
Search for 18:
index 5 → 18 ✅
✅ Found in 1 comparison
Search for 33:
Middle = index 5 → 18
33 > 18 → Search right: index 6–10
Middle = index 8 → 27
33 > 27 → Search right: index 9–10
Middle = index 9 → 30
33 > 30 → index 10 → 33 ✅
Search for 5:
Middle = index 5 → 18
5 < 18 → Search left: index 0–4
Middle = index 2 → 9
5 < 9 → Search left: index 0–1
Middle = index 0 → 3
5 > 3 → index 1 → 6
5 < 6 → No more to search
❌ Not found in 5 comparisons
🔠 PART D: Binary Search with Strings List: [“apple”, “banana”, “carrot”, “dragonfruit”, “fig”, “grape”, “kiwi”, “mango”, “orange”, “peach”, “watermelon”]
Search for "mango":
Middle = index 5 → "grape"
"mango" > "grape" → Search right: index 6–10
Middle = index 8 → "orange"
"mango" < "orange" → index 6–7
Middle = index 6 → "kiwi"
"mango" > "kiwi" → index 7 → "mango" ✅
✅ Found in 4 comparisons
Search for "carrot":
Middle = index 5 → "grape"
"carrot" < "grape" → index 0–4
Middle = index 2 → "carrot" ✅
✅ Found in 2 comparisons
Search for "lemon":
Middle = index 5 → "grape"
"lemon" > "grape" → index 6–10
Middle = index 8 → "orange"
"lemon" < "orange" → index 6–7
Middle = index 6 → "kiwi"
"lemon" > "kiwi" → index 7 → "mango"
"lemon" < "mango" → Not found
❌ Not found in 5 comparisons
✅ Free Response Questions Q1: Why is binary search more efficient for large data than linear search? Binary search cuts the list in half each time, reducing the number of steps needed. This makes it much faster than checking every item like linear search, especially for big lists.
Q2: What happens if the list isn’t sorted and you try to use binary search? If the list isn’t sorted, binary search won’t work correctly because it relies on order.
Q3: Could you use binary search in a video game leaderboard or streaming service search bar? Why or why not? Yes, if the leaderboard or search results are sorted, binary search can quickly find names or titles.