Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a...
Transcript of Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a...
![Page 1: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/1.jpg)
Recursion + Binary SearchKelly Rivers and Stephanie Rosenthal
15-110 Fall 2019
![Page 2: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/2.jpg)
Announcements
• Homework 3 Check-in 2• How did it go?
• Homework 3 full is due next week!
![Page 3: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/3.jpg)
Learning Objectives
• To understand the purpose and definition of recursion
• To produce recursive algorithms
• To understand and trace a recursive binary search algorithm
![Page 4: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/4.jpg)
Recursion
Recursion is a way to compute by
1) breaking down the problem into smaller pieces
2) calling itself (the same function) on each smaller piece
3) combining the smaller answers back together in some way
![Page 5: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/5.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
![Page 6: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/6.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
Return 1+
![Page 7: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/7.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
Return 1+ recursiveAdd( )2 3
Return 2+
![Page 8: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/8.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
Return 1+ recursiveAdd( )2 3
Return 2+recursiveAdd( )3
Return 3+
![Page 9: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/9.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
Return 1+ recursiveAdd( )2 3
Return 2+recursiveAdd( )3
Return 3+ recursiveAdd( [] ) Return 0
![Page 10: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/10.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
Return 1+ recursiveAdd( )2 3
Return 2+recursiveAdd( )3
Return 3+ 0
![Page 11: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/11.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
Return 1+ recursiveAdd( )2 3
Return 2+recursiveAdd( )3
3+0 = 3
![Page 12: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/12.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
Return 1+ recursiveAdd( )2 3
2+3 = 5
![Page 13: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/13.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
Return 1+5 = 6
![Page 14: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/14.jpg)
Picture of What is Happening
Example:
def recursiveAdd(L):
if L == []:
return 0
return L[0]+recursiveAdd(L[1:])
recursiveAdd( )1 2 3
6
![Page 15: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/15.jpg)
Recursion
General Form:
def recursiveFunction(X):
if X == ?: #base case is some smallest case
return ____ #something not recursive
else:
#call itself on a smaller piece
result = recursiveFunction(Xsmaller)
#combine with some value and return
return _______
![Page 16: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/16.jpg)
Activity: Search Elements in List
def recursiveSearch(L,item):
![Page 17: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/17.jpg)
Activity: Search Elements in List
def recursiveSearch(L,item):
Base cases:
L = [], what should be returned?
L[0] == item, what should be returned?
Inductive case?
![Page 18: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/18.jpg)
Activity: Search Elements in List
def recursiveSearch(L,item):
if len(L) == 0: #base case is some smallest case
return False
if L[0] == item: #base case is some smallest case
return True
else:
#call itself on a smaller piece
result = recursiveSearch(L[1:],item)
return result
![Page 19: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/19.jpg)
Trace the function
def recursiveSearch(L,item):
if len(L) == 0: #base case is some smallest case
return False
if L[0] == item: #base case is some smallest case
return True
else:
#call itself on a smaller piece
result = recursiveSearch(L[1:],item)
return result
recursiveSearch(["dog", "cat", "mouse"], "mouse")
![Page 20: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/20.jpg)
Trace the function
def recursiveSearch(L,item):
if len(L) == 0: #base case is some smallest case
return False
if L[0] == item: #base case is some smallest case
return True
else:
#call itself on a smaller piece
result = recursiveSearch(L[1:],item)
return result
recursiveSearch([0,10,30,60,200,500],65)
![Page 21: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/21.jpg)
Searching Sorted Lists
Linear Search: searching in order from beginning to end
I need 10 comparisons to check for 98
2 5 10 20 42 56 67 76 89 95
Start here
![Page 22: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/22.jpg)
Searching Sorted Lists
Linear Search: searching in order from beginning to end
I need 10 comparisons to check for 98
Different Idea: I can eliminate half of the list with 1 comparison if I check the middle element instead of the first element
2 5 10 20 42 56 67 76 89 95
2 5 10 20 42 56 67 76 89 95
Start here
Start here
![Page 23: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/23.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
2 5 10 20 42 56 67 76 89 95
10 elements. Start at index 5
Search for 95
2 5 10 20 42 67 76 89 9556
Everything smaller than L[5] Everything larger than L[5]
Check if 95 == L[5]
95 is not L[5] AND it can’t be in the list of smaller elementsRecurse on only the list w/larger elements
![Page 24: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/24.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
2 5 10 20 42 56 67 76 89 95
10 elements. Start at index 5
Search for 95
2 5 10 20 42 67 76 89 9556
Everything smaller than L[5] Everything larger than L[5]
Check if 95 == L[5]
95 is not L[5] AND it can’t be in the list of smaller elementsRecurse on only the list w/larger elements
67 76 89 95
4 elements. Start at index 2
9567 76 89
Everything smaller than L[2] Everything larger than L[2]
Check if 95 == L[2]
95 is not L[2] AND it can’t be in the list of smaller elementsRecurse on only the larger list
Search for 95
![Page 25: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/25.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
67 76 89 95
4 elements. Start at index 2
9567 76 89
Everything smaller than L[2] Everything larger than L[2]
Check if 95 == L[2]
95 is not L[2] AND it can’t be in the list of smaller elementsRecurse on only the larger list
Search for 95
![Page 26: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/26.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
67 76 89 95
4 elements. Start at index 2
9567 76 89
Everything smaller than L[2] Everything larger than L[2]
Check if 95 == L[2]
95 is not L[2] AND it can’t be in the list of smaller elementsRecurse on only the larger list
Search for 95
95
1 element. Start at index 0
95
Everything smaller than L[0] Everything larger than L[0]
Check if 95 == L[0]
RETURN TRUE
[][]
Search for 95
![Page 27: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/27.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
3 checks instead of 10!
67 76 89 95
4 elements. Start at index 2
9567 76 89
Everything smaller than L[2] Everything larger than L[2]
Check if 95 == L[2]
95 is not L[2] AND it can’t be in the list of smaller elementsRecurse on only the larger list
Search for 95
95
1 element. Start at index 0
95
Everything smaller than L[0] Everything larger than L[0]
Check if 95 == L[0]
RETURN TRUE
[][]
Search for 95
![Page 28: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/28.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
2 5 10 20 42 56 67 76 89 95
10 elements. Start at index 5
Search for 15
2 5 10 20 42 67 76 89 9556
Everything smaller than L[5] Everything larger than L[5]
Check if 15 == L[5]
15 is not L[5] AND it can’t be in the list of larger elementsRecurse on only the list w/smaller elements
![Page 29: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/29.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
2 5 10 20 42 56 67 76 89 95
10 elements. Start at index 5
Search for 15
2 5 10 20 42 67 76 89 9556
Everything smaller than L[5] Everything larger than L[5]
Check if 15 == L[5]
15 is not L[5] AND it can’t be in the list of larger elementsRecurse on only the list w/smaller elements
5 elements. Start at index 2
2 5 10
Everything smaller than L[2] Everything larger than L[2]
Check if 15 == L[2]
15 is not L[2] AND it can’t be in the list of smaller elementsRecurse on only the larger list
Search for 152 5 10 20 42
20 42
![Page 30: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/30.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
5 elements. Start at index 2
2 5 10
Everything smaller than L[2] Everything larger than L[2]
Check if 15 == L[2]
15 is not L[2] AND it can’t be in the list of smaller elementsRecurse on only the larger list
Search for 152 5 10 20 42
20 42
![Page 31: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/31.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
2 elements. Start at index 1
42
Everything smaller than L[1] Everything larger than L[1]
Check if 15 == L[1]Recurse on only the list of smaller elements
[]
Search for 15
5 elements. Start at index 2
2 5 10
Everything smaller than L[2] Everything larger than L[2]
Check if 15 == L[2]
15 is not L[2] AND it can’t be in the list of smaller elementsRecurse on only the larger list
Search for 152 5 10 20 42
20 42
20 42
20
![Page 32: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/32.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
Search for 15
2 elements. Start at index 1
42
Everything smaller than L[1] Everything larger than L[1]
Check if 15 == L[1]Recurse on the list of smaller elements
[]
20 42
20
![Page 33: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/33.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
1 element. Start at index 0
Search for 15
Search for 15
20
20
Everything smaller than L[0] Everything larger than L[0]
Check if 15 == L[0]Recurse on smaller elts
[][]
2 elements. Start at index 1
42
Everything smaller than L[1] Everything larger than L[1]
Check if 15 == L[1]Recurse on the list of smaller elements
[]
20 42
20
![Page 34: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/34.jpg)
Binary Search
For a sorted list, compare to the middle element and recurse on the side that the element is on
1 element. Start at index 0
Search for 15
Search for 15
20
20
Everything smaller than L[0] Everything larger than L[0]
Check if 15 == L[0]Recurse on smaller elts
[][]
2 elements. Start at index 1
42
Everything smaller than L[1] Everything larger than L[1]
Check if 15 == L[1]Recurse on the list of smaller elements
[]
20 42
20
[]
Empty list. RETURN FALSE
Search for 15
![Page 35: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/35.jpg)
Binary Search: Describe in your own words
Describe what is happening in 30 words or less
![Page 36: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/36.jpg)
Binary Search: Base Cases
What are the base cases?
![Page 37: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/37.jpg)
Binary Search: Base Cases
What are the base cases?
If the list is empty, we know NOTHING can be in the list. Return False
If the middle element is our element, we know we found it. Return True
![Page 38: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/38.jpg)
Binary Search
def binarySearch(L,item):
if len(L) == 0: #base case is some smallest case
return False
middle = len(L)//2
if L[middle] == item: #base case is some smallest case
return True
Base Cases
![Page 39: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/39.jpg)
Binary Search: Induction Step
If the list is not empty, and the middle element isn’t the right one,
What is the induction or recursive step?
![Page 40: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/40.jpg)
Binary Search: Induction Step
If the list is not empty, and the middle element isn’t the right one,
Determine if the element would be in the list of smaller elements or the list of larger elements, and recurse on that sub-list.
![Page 41: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/41.jpg)
Binary Search
def binarySearch(L,item):
if len(L) == 0: #base case is some smallest case
return False
middle = len(L)//2
if L[middle] == item: #base case is some smallest case
return True
elif L[middle] > item:
return binarySearch(L[:middle],item)
else:
return binarySearch(L[middle+1:],item)
Base Cases
InductionStep
![Page 42: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/42.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 95 )
Return
2 5 10 20 42 56 67 76 89 95
![Page 43: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/43.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 95 )
Return binarySearch( , 95 )
Return
2 5 10 20 42 56 67 76 89 95
67 76 89 95
![Page 44: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/44.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 95 )
Return binarySearch( , 95 )
Return binarySearch( , 95)95
Return
2 5 10 20 42 56 67 76 89 95
67 76 89 95
![Page 45: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/45.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 95 )
Return binarySearch( , 95 )
Return binarySearch( , 95)95
Return True
2 5 10 20 42 56 67 76 89 95
67 76 89 95
![Page 46: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/46.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 95 )
Return binarySearch( , 95 )
Return True
2 5 10 20 42 56 67 76 89 95
67 76 89 95
![Page 47: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/47.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 95 )
Return True
2 5 10 20 42 56 67 76 89 95
![Page 48: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/48.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return
2 5 10 20 42 56 67 76 89 95
![Page 49: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/49.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return binarySearch( , 15 )
Return
2 5 10 20 42 56 67 76 89 95
2 5 10 20 42
![Page 50: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/50.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return binarySearch( , 15 )
Return binarySearch( , 15 )
Return
2 5 10 20 42 56 67 76 89 95
2 5 10 20 42
20 42
![Page 51: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/51.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return binarySearch( , 15 )
Return binarySearch( , 15 )
Return
2 5 10 20 42 56 67 76 89 95
2 5 10 20 42
20 42
binarySearch( , 15 )20
Return
![Page 52: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/52.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return binarySearch( , 15 )
Return binarySearch( , 15 )
Return
2 5 10 20 42 56 67 76 89 95
2 5 10 20 42
20 42
binarySearch( , 15 )
binarySearch([],15) Return False
20
![Page 53: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/53.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return binarySearch( , 15 )
Return binarySearch( , 15 )
Return
2 5 10 20 42 56 67 76 89 95
2 5 10 20 42
20 42
binarySearch( , 15 )20
Return False
![Page 54: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/54.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return binarySearch( , 15 )
Return binarySearch( , 15 )
Return False
2 5 10 20 42 56 67 76 89 95
2 5 10 20 42
20 42
![Page 55: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/55.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return binarySearch( , 15 )
Return False
2 5 10 20 42 56 67 76 89 95
2 5 10 20 42
![Page 56: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/56.jpg)
Trace Binary Search
def binarySearch(L,item): #newlines removed for space
if len(L) == 0: return False
middle = len(L)//2
if L[middle] == item: return True
elif L[middle] > item: return binarySearch(L[:middle],item)
else: return binarySearch(L[middle+1:],item)
binarySearch( , 15 )
Return False
2 5 10 20 42 56 67 76 89 95
![Page 57: Recursion + Binary Search15110-f19/slides/week7-1-binarysearch.pdf · •To understand and trace a recursive binary search algorithm. Recursion Recursion is a way to compute by 1)](https://reader034.fdocuments.in/reader034/viewer/2022050215/5f618478efea1e6a8f2b4c99/html5/thumbnails/57.jpg)
Big Picture
If we can make an assumption about what our list looks like, we can search faster (with fewer comparisons) than linear search.
- Dividing in half is a really good way to reduce the comparisons
- Recursion allows us to solve the problem on half the size
Next time we’ll talk about how to formally make those comparisons between different implementations of algorithms.