Searching Algorithm
Jed Rembold and Fred Agbo
April 15, 2024
Announcements
- Personal Project is due tonight at 10 pm!
- If you are asking for extension, send me an email.
- I will check to see you have not used your 3 chances before granting
it.
- Project 5 Adventure guidelines is posted. Due on
Monday 29th April at 10 pm
- You Can work with a partner on this project. Both
partners will earn the same grade.
- To accept this project, you must great a team
even if you are not working with a
partner
- If you are working with a partner, One person create the term, the
second will join
- Otherwise, use your regular GitHub username as the team name
- NO CLASS on Wednesday this week due to SSRD program
- However, the section meeting will hold. Please try to attend!
- Polling: https://www.polleverywhere.com/agbofred203
Review Question!
The two classes to the right mimic a bit of what might occur in the
course of the Adventure Project. What python data type is
ANS
?
- A String
- An AdvObject
- An AdvLocation
- A List
class AdvObject:
def __init__(self, name, loc):
self.name = name
self.loc = loc
def get_loc(self):
return self.loc
class AdvLocation:
def __init__(self, name):
self.name = name
self.stuff = []
def store(self, item):
self.stuff.append(item)
def retrieve_top(self):
return self.stuff.pop()
A = AdvObject("Hammer", "RA")
B = AdvObject("Torch", "RA")
RA = AdvLocation("Room A")
RA.store(A)
RA.store(B)
ANS = RA.retrieve_top().get_loc()
Searching for Efficiency
- Our last chapter is less about introducing new programming machinery
and more about better understanding what we already have
- Hopefully you have realized by now that there can be
many approaches to solving a problem computationally
- So far, the first way you figure out has likely been the “best”, in
that it gets the job done.
- Sometimes there is a different though in an approach that is
technically correct and one that is practically correct.
- How can we make informed choices about the algorithms we use?
- Want to look at algorithm efficiency in this chapter
- Will focus mainly on Searching and Sorting as our examples to better
understand how an algorithm’s efficiency can be quantified
A Linear Search
Suppose you needed to determine if a particular element was in a
list, and didn’t have any of the built-in methods available to
you
The easiest method (which many of you have indeed used!) is to
just search through the list element by element and check it to see if
it is the one you desire
- This approach is called a linear search
Easy to understand and implement:
def linear_search(quest, array):
for i in range(len(array)):
if array[i] == quest:
return i
return -1
Linear Search from English Words
- Assuming we would like to search for a word in the list of
ENGLISH_WORDS
- The Python strategy using
is_english_word
would below
def is_english_word(s):
s = s.lower()
for i in range(len(ENGLISH_WORDS)):
if s == ENGLISH_WORDS[i]:
return True
return False
- The for loop begins at the beginning and continues until it comes to
the end of the ENGLISH_WORDS array.
- The function returns
True
when it finds
the word in the array or, False
when it
fails to find the word at the end of the for loop.
Searching for Area Codes
- To illustrate the efficiency of linear search, it can be helpful to
work with a larger dataset
- We’ll look here at searching through potential US area codes to find
that of Salem: 503
- Linear search examines each value in order to find the matching
value.
- As the arrays get larger, the number of steps required also
grows
- As you watch linear search do its thing on the next slide, see if
you can beat the computer at finding 503.
- What approach did you take?
How did you do?
- Frequently, many people can “beat the animation” in finding 503
- Approaches vary, but you may well have done something along the
lines of:
- Look at some number in the middle
- Depending on how close it was to 503, jump ahead some in that
direction and check again
- Requires some special conditions though, so let’s try again
Racing Linear Search Again
Idea of a Binary Search
- If your data is ordered, then you might try a alternative search
strategy
- Look at the center element in the array, it is either:
- The value you want. Excellent! Return it.
- A value larger than what you want. Throw away that value and
everything bigger.
- A value smaller than what you want. Throw away that value and
everything smaller.
- Then you can repeat the process with the remaining elements until
you find your value
- Since number of searched elements is divided by 2 each time, called
a binary search
Implementing Binary Search
def binary_search(target, array):
lh = 0
rh = len(array) - 1
while lh <= rh:
middle = (lh + rh) // 2
if array[middle] == target:
return middle
elif array[middle] < target:
lh = middle + 1
else:
rh = middle - 1
return -1
Sorting
- Binary search only works on arrays in which the elements are
ordered.
- The process of putting the elements into order is called
sorting.
- Lots of different sorting algorithms, which can vary substantially
in their efficiency.
- From an algorithms view, sorting is probably the most applicable
algorithm we’ll discuss in this course
- Organizing data makes it easier to digest that data, whether the
data is being digested by other machines or by humans
Selection Sort
- The easiest sorting algorithm to explain is that of selection
sort
- Imagine your left hand keeping track of where you were in the array,
and your right hand scanning through and finding the next smallest value
to move to that location each iteration
def selection_sort(array):
for lh in range(len(array)):
rh = lh
for i in range(lh+1, len(array)):
if array[i] < array[rh]:
rh = i
array[lh], array[rh] = array[rh], array[lh]
Following Selection Sort
def selection_sort(array):
for lh in range(len(array)):
rh = lh
for i in range(lh+1, len(array)):
if array[i] < array[rh]:
rh = i
array[lh], array[rh] = array[rh], array[lh]
L = [31, 41, 59, 26, 53, 58, 97, 93, 23, 84]
selection_sort(L)