The Search for Efficiency
Jed Rembold
November 21, 2025
Announcements
- I actually got you Exam 2 results! gasp More on those in
just a second
- Be working on the Infinite Adventure Project!
- You have everything you need to complete it at this point
- Final
- I will be posting final study materials and an old test a week from
tomorrow so that you can start studying
- Will be more weighted toward content from the latter half of the
semester, but everything we’ve been doing this semester builds on
earlier material
- Adventure largely marks the end of testable material, though content
we cover between now and the final may show up in some extra credit
contexts
- I will be giving you time on the last day of class to complete our
course course evals (the SAIs)
Midterm 2 Debrief
- Final breakdown:
- Average: 71.26%
- Median: 83.08%
- St Dev: 26.26%
Exam Corrections
- Given this situation, I am going to offer some exam corrections for
my first time in teaching this course
- If you lost points on a non-extra credit problem, you can earn up to
50% of the lost points back by:
- Creating a Trilogy Portfolio: Collecting learning
objectives, identifying past examples in in-class work and in your work,
synthesizing why you didn’t make the connections and how to improve
going forwards
- Creating a new problem and solution: New
problem should address at least 80% of your identifying learning
objectives, and your solution should be correct. Must also write a
“Design Rationale” paragraph about why you made the choices you did in
writing your solution.
- Full guide can be found here
- Must be completed for each problem you want points
back on
- Due midnight of Dec 5. Past that point, you will not be able to
submit.
- 100% optional, and you can not earn over a 100% by doing
corrections
Helping Fellow Students
- Students from the STAT-365 (Statistical Engineering) are working on
final projects.
- One is investigating student use of flex dollars.
- They are interested in all submissions, but particularly would
appreciate more insight from freshman.
- Consider helping them out when you have a brief moment (like after
you complete the polling question in just a moment!)
Review Activity!
- Last class we introduced the
requests
library, and you had a chance to use it in section this week
- Didn’t get it installed? Downloading and running this may be easiest if
pip doesn’t like you…
- Your attendance for today is to send a very simple payload with only
a “name” key (and your name as the corresponding value) to the URL
below:
- Should get a 201 status code in return if your name has been
recorded in today’s attendance!
- If you get something else, try again!
- Ask me if you are struggling!
Searching for Efficiency
- Chapter 8 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.
- There can be a difference in an approach that is technically correct
and one that is practically correct though.
- 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(target, array):
for i in range(len(array)):
if array[i] == target:
return i
return -1
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, this
method is 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
Linear Search Efficiency
- The running time of the linear search depends on the size of the
array
- That in itself is not particularly surprising. The running time of
most algorithms will depend on the size of the problem to which the
algorithm is applied.
- For many applications, it is easy to come up with a numeric value
that describes the problem size, commonly called \(N\).
- For most lists, \(N\) is simply the
length of the array
- In the worst case, when the target value is the last element of the
list or does not appear at all, the linear search requires \(N\) steps
- On average, it takes about half that, or \(\frac{N}{2}\)
- Computer scientists are pessimists though, and will generally use
the worse case scenario to compare
Binary Search Efficiency
- The running time of binary search also depends on the size of the
array, but in a very different way
- Each step of the process, the binary search rules out half the
remaining options
- The worst case (which we had earlier!) requires a number of steps
equal to however many times we can divide the array in half until we
have only a single number left.
- Mathematically, this looks like \[1 = N /
\underbrace{2 / 2 / 2 / 2 \cdots / 2}_{k\text{ times}} =
\frac{N}{2^k}\]
- We really want to know the number of steps, \(k\), so solving for \(k\): \[2^k = N
\quad\Rightarrow\quad k = \log_2(N)\]
Comparing Efficiencies
- The below table illustrates the differences in the number of
required steps for the two search algorithms
| Problem Size |
Linear (\(N\)) |
Binary (\(log_2 N\)) |
| 10 |
10 |
3 |
| 100 |
100 |
7 |
| 1,000 |
1,000 |
10 |
| 1,000,000 |
1,000,000 |
20 |
| 1,000,000,000 |
1,000,000,000 |
30 |
- Clearly, for large values, the difference in the number of steps is
enormous
- At 1 million elements, the binary search is 50,000 times
faster!
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