0% found this document useful (0 votes)
39 views32 pages

Binary Search G5 - No Code

This document provides an overview of the binary search algorithm. It begins with learning objectives and a lecture flow that includes prerequisites, introduction, naive approach, binary search explanation, variants, common pitfalls, and a quote. The key points are: - Binary search is an efficient algorithm for searching sorted data by cutting the search space in half at each step. - It improves on the naive linear search approach which takes O(n) time by taking O(log n) time in the worst case. - It works by selecting the middle element of the search range at each step, and either restricting the search space to above or below the middle based on if the target is found. - There are variants for searching

Uploaded by

Fedasa Bote
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views32 pages

Binary Search G5 - No Code

This document provides an overview of the binary search algorithm. It begins with learning objectives and a lecture flow that includes prerequisites, introduction, naive approach, binary search explanation, variants, common pitfalls, and a quote. The key points are: - Binary search is an efficient algorithm for searching sorted data by cutting the search space in half at each step. - It improves on the naive linear search approach which takes O(n) time by taking O(log n) time in the worst case. - It works by selecting the middle element of the search range at each step, and either restricting the search space to above or below the middle based on if the target is found. - There are variants for searching

Uploaded by

Fedasa Bote
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Binary

Search
Learning Objective
1. Understanding the Binary Search Algorithm
2. Understanding why the algorithm’s e cient
3. Understanding the Algorithm's Requirements
4. Exploring Applications and Use Cases.

2
Lecture Flow
1. Prerequisites
2. Introduction
3. Naive Approach
4. Binary Search
5. Variants
6. Common Pi alls
7. Quote of the day

3
Pre-requisites
1. Array
2. Sorting
3. Loops and conditional statements

4
Introduction
Let’s play a game.

5
Introduction
● Binary Search is one of the most fundamental and useful algorithms in
Computer Science.
● It describes the process of searching for a specific value in an ordered
collection.

6
Naive Approach

Given a sorted list of numbers search for 6

1 2 3 4 5 6 7 8

7
Naive Approach

You iterate until we find the number. It takes 6 steps.

1 2 3 4 5 6 7 8

8
Naive Approach - Time Complexity

Worst case scenario: when the element to be searched is at the end. Eg:
when target is 8. O(n)

1 2 3 4 5 6 7 8

9
Binary Search

We know the numbers are sorted. Is there a room for optimization?

1 2 3 4 5 6 7 8

10
Binary Search

Let’s call the region of the list we are looking for the number in the search
space. We will start with the whole list as the search space.

1 2 3 4 5 6 7 8

11
Binary Search

Let’s pick a number around the middle. If the picked number is smaller than the
target number, we make the search space after the picked number.

Otherwise, we make the search space until this number.

1 2 3 4 5 6 7 8

12
Binary Search

Let’s pick a number around the middle. If the picked number is smaller than the
target number, we make the search space after the picked number.

Otherwise, we make the search space until this number.

1 2 3 4 5 6 7 8

13
Binary Search

Let’s pick a number around the middle. If the picked number is smaller than the
target number, we make the search space after the picked number.

Otherwise, we make the search space until this number : 3 steps

1 2 3 4 5 6 7 8

14
Practice

Binary Search
Binary Search - Time Complexity
What is the number of steps needed to make the search space size exactly 1?
On each iteration we are halving it.

1 2 3 4 5 6 7 8
1*2*2* … *2 = problem size
Number of steps
Times
1 2 3 4 5 6 7 8

2^(number of steps) = problem size


1 2 3 4 5 6 7 8
Number of steps = log2(problem size)
1 2 3 4 5 6 7 8

16
Note
mid = (high + low) / 2

Unlike in python, this could result in an overflow, in lower-level languages. So


use this instead.

mid = low + (high - low) // 2

17
A more general way of thinking
Not all binary search problems are about finding the right position in a sorted
list.

Problem Link

18
A more general way of thinking
In this problem we are looking for the first time we get a defect.

Not Not Not Not Not


Defect Defect Defect
Defect Defect Defect Defect Defect

19
A more general way of thinking
Cut the search space by half every time

Not Not Not Not Not


Defect Defect Defect
Defect Defect Defect Defect Defect

20
A more general way of thinking
Cut the search space by half every time

Not Not Not Not Not


Defect Defect Defect
Defect Defect Defect Defect Defect

21
A more general way of thinking
Cut the search space by half every time

Not Not Not Not Not


Defect Defect Defect
Defect Defect Defect Defect Defect

22
A more general template

def binarySearch(low=1, high=1, key = lambda x: True):

while low < high:


mid = low + (high - low)//2
if key(mid):
high = mid
else:
low = mid+1
return low

23
Variants

24
Variant 1 - Over an input space

This is the variant we have seen, where we search for a particular value in
the given input.

In other words, the search space is given explicitly by the problem.

25
Variant 1 - Over an input space

Pair Programming
Problem Link

26
Variant 2 - Over an output space

The search is applied over a possible output range. For every choice
there is usually a linear check to validate the choice.

This is trickier than the first variant.

27
Variant 2 - Over an output space

Pair Programming
Problem Link

28
Common Pitfalls

29
Common Pitfalls

● Not checking the right condition for loop termination;


low < high vs low <= high vs low + 1 < high
● Off-by-one on the search space
● Unreachable loop-termination condition
Practice Problems

1. H-Index II - LeetCode
2. Search a 2D matrix
3. Heaters
4. The Meeting Place Cannot Be Changed
Quote of the Day

“Believe you can and you're halfway there.”


~ Theodore Roosevelt

You might also like