![Page 1: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/1.jpg)
Linear SearchBinary Search Tree
Revised based on textbook author’s notes.
![Page 2: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/2.jpg)
2
Searching
The process of selecting particular information from a collection of data based on specific criteria.
Can be performed on different data structures.
sequence search – search within a sequence.
search key (or key) – identifies a specific item.
compound key – consists of multiple parts.
![Page 3: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/3.jpg)
3
Linear Search
Iterates over the sequence, item by item, until the specific item is found or the list is exhausted.
The simplest solution to sequence search problem.
Python's in operator: find a specific item.if key in theArray :
print( 'The key is in the array.' )
else :
print( 'The key is not in the array.' )
![Page 4: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/4.jpg)
4
Linear Search Examples
Searching for 31
Searching for 8
![Page 5: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/5.jpg)
5
Linear Search Code
def linear_search( the_values, target ) :
n = len( the_values )
for i in range( n ) :
if the_values[i] == target
return True # or return I
return False # or return -1
![Page 6: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/6.jpg)
6
Linear Search: Sorted Sequence
A linear search can be performed on a sorted sequence.
Example: searching for 8.
![Page 7: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/7.jpg)
7
Linear Search: Sorted Sequence
def sorted_linear_search( the_values, target ) :
n = len( the_values )
for i in range( n ) :
if the_values[i] == target :
return True
elif the_values[i] > target :
return False
return False
Similar to the unsorted sequence, with one major difference.
![Page 8: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/8.jpg)
8
Linear Search: Smallest Value
We can search for an item based on certain criteria.
Example: find the smallest value.
def find_smallest( the_values ):
n = len( the_values )
smallest = the_values[0]
for i in range( 1, n ) :
if the_values[i] < smallest :
smallest = the_values[i]
return smallest
![Page 9: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/9.jpg)
9
Binary Search
The linear search has a linear time-complexity.
We can improve the search time if we modify the search technique itself.
Use a divide and conquer strategy.
Requires a sorted sequence.
![Page 10: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/10.jpg)
10
Binary Search Algorithm
Examine the middle item (searching for 10):
One of three possible conditions: target is found in the middle item.
target is less than the middle item.
target is greater than the middle item.
![Page 11: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/11.jpg)
11
Binary Search Algorithm
Since the sequence is sorted, we can eliminate half the values from further consideration.
![Page 12: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/12.jpg)
12
Binary Search Algorithm
Repeat the process until either the target is found or all items have been eliminated.
![Page 13: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/13.jpg)
13
Binary Search Implementation
def binary_search( the_values, target ) :
low = 0
high = len(the_values) - 1
while low <= high :
mid = low + (high - low) // 2
if the_values[mid] == target :
return True # or return mid
elif target < the_values[mid] :
high = mid - 1
else :
low = mid + 1
return False # or return -1
![Page 14: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/14.jpg)
14
Binary Search Implementation
![Page 15: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/15.jpg)
Your Exercise
• Implement binary search in recursion
![Page 16: Linear Search Binary Search Tree - Bucknell Universitycsci204/2017-fall/meng/notes/32_Search/32_… · Linear Search Binary Search Tree Revised based on textbook author’s notes](https://reader034.vdocuments.site/reader034/viewer/2022042210/5eae0066e46fbe29f835a7d5/html5/thumbnails/16.jpg)
Suggested solution
def binary_search( the_list, key, low, high ) :
if low > high:
return False # or return -1
mid = low + (high – low) // 2
if the_list[mid] == key :
return True # or return mid
elif key < the_values[mid] :
return binary_search( the_list, key, low, mid-1 )
else :
return binary_search( the_list, key, mid+1, high )