T

Binary Search Algorithm

Computer Science
Grade 8
Teacher Script & Notes
What to say / Spoken Script
Hello everyone, today we are going to learn the Binary Search algorithm. Here we have a sorted array of numbers, and we want to find the number 7. The key to binary search is that we set up two pointers: a 'left' pointer representing the start of our search range, located at the far left; and a 'right' pointer representing the end of our range, located at the far right. The current search range is highlighted in blue.
What to do / Action Guide
Observe the array: [2, 3, 5, 7, 11, 13, 17], and the red highlight showing Target: 7. The left and right pointers are initialized at index 0 and 6 respectively.
Pedagogical Tips
Emphasize to students: Binary search only works on sorted arrays. If the array is unsorted, the algorithm cannot be applied.
What to say / Spoken Script
In the second step, we calculate the middle element between the left and right pointers. Here, left points to index 0 and right points to index 6. The midpoint formula is mid = (left + right) / 2, which gives us 3. The element at index 3 is 7. The 'mid' pointer now points directly to this element.
What to do / Action Guide
Observe the yellow Mid pointer pointing to index 3 (which holds the value 7).
Pedagogical Tips
Ask students: How do we calculate the mid pointer if the search range has an even number of elements? (Typically, we round down using integer division).
What to say / Spoken Script
Finally, we compare the value pointed to by mid (7) with our search Target (7). We find that they are exactly equal! Therefore, we have successfully found our target value. The search process ends here. It was extremely efficient, taking only a single comparison!
What to do / Action Guide
Notice that the Mid pointer turns green, indicating a successful match. The navigation controls will now disable the 'Next Step' button, allowing only 'Prev Step' for review.
Pedagogical Tips
Ask students: What if the value was not equal? For example, if we were searching for 13, what would be the next step? (We would move the left pointer to mid + 1, which is 4, and continue searching in the right half of the array).
Step 1: Introduction
Step 1 of 3
2 3 5 7 11 13 17 L (0) R (6) Target: 7
2 3 5 7 11 13 17 L (0) M (3) R (6) Target: 7
2 3 5 7 11 13 17 L (0) Found! (3) R (6) Target: 7 (Found)
Was this lesson helpful? Rate this Course
Discussion
Loading comments...