[VIDEO PLAYBACK]
- It feels like we need a good algorithm
for actually doing that sorting.
So how can we go about doing that?
[END PLAYBACK]
DAVID MALAN: So one of my favorite
things to do in lecture each year
is I actually have these upcoming demos,
where we invite some eight students up
on stage and actually enact
various sorting algorithms.
And I'll admit I've actually
struggled with how best to do this.
Because my instincts
always are to just invite
the audience to start telling us
what to do, how to sort yourselves.
But it never leads necessarily
to the best pedagogy to be honest
or the best narrative, the best order
of operations, because sometimes we
sort of organically end up with
selection's sort or insertion
sort or bubble sort or
something else altogether.
And then I have to sort
of wrestle with it, OK,
how do I rein this in and
try to clean it up a bit.
And so on other years, I very
deliberately advise students
on how to sort themselves.
And I've said, all right what if
you go here and then you go here.
And that's kind of given a better
result in terms of the flow that I want,
but it's a tension I feel honestly.
DOUG LLOYD: It can be tricky to
have to open things up like this.
And this is actually something we saw
at the very beginning of the course
when we were learning about how the
English language can actually be really
bad for trying to explain what to do.
We can sort of run into these
situations where we are unintentionally
imprecise with our language.
And I feel like when
we take the time to--
when we do script this
a little bit more.
And we do for students and do
things in a particular way,
it allows us to speak
much more precisely
about the way algorithms work.
And because algorithms are step
by step set of instructions
that are very precise, they don't
work if you have imprecision.
And so--
DAVID MALAN: And I would try both ways,
honestly, and try both ways in a class,
maybe alternating years or
different classes of students
and see just which one works best.
I will say, using music
stands or some equivalent,
it was kind of an innovation
or an improvement at some point
when we discovered we had
access to eight music stands.
Because the algorithms that require
shifting, so like insertion sort,
for instance, where you want
to shift a lot of the students,
it's harder to see the
expense of that unless there
are these preordained positions
that the students have to stay in.
DOUG LLOYD: And have to physically lift
this device and do this extra work,
it makes it much more clear.
DAVID MALAN: Or even just the piece of
paper, like the music stands can stay.
DOUG LLOYD: Oh, that's true.
DAVID MALAN: Just noticing
that these are now
eight spots that are finite in
number, but fixed in location,
you see the students more deliberately
incurring the cost of moving.
Whereas in the past, people
would very politely just kind of
make room for someone.
And it's hard to formalize what
was that expense when you just
made room for someone right there.
DOUG LLOYD: Yeah.
I think showing these--
this is definitely an example
where having an in-person demo
is really high value.
DAVID MALAN: I think so.
DOUG LLOYD: It's worth
taking the time in class
to spend even maybe a full class period
or two class periods just doing this
and showing these examples off.
Because it's really
high impact, I think.
DAVID MALAN: And I will
say was this is just
a historical accident
that the even numbers are
on the left and the odd
numbers on the right.
I'm not quite sure
how that came to pass,
but I've been using the same numbers
for historical sake, history's sake
again and again.
But that probably adds
a bit of undue curiosity
that we should probably
eliminate next year honestly.
Because there is no reason for that.
DOUG LLOYD: Actually, I
think not too long ago,
we had a situation where we were
using these numbers for merge sort
and we inadvertently created a situation
where things were already merged--
DAVID MALAN: Yeah, that was
not a very enlightening day.
DOUG LLOYD: --as a result.
DAVID MALAN: But those three algorithms
are very deliberately chosen.
Bubble sort, I love the simplicity
of it and the iterative approach
to solving the problem.
It's getting better and
better and better and better
and better and better.
And it's taking a while
to get there, but you
can see the improvement,
which each of those swaps.
Selection Source is nice because
it's just so darn simple.
Just go get me the next
smallest element, bring it here.
Bring it here.
Bring it here.
There's this repetition to
that that's very accessible.
Insertion sort, I personally tend
to do third among these three
because it requires a
little bit more thought.
There's a little more
cleverness in that you're
going from left to right, dealing
with each element in turn,
but then solving the problem
by shifting people as needed.
DOUG LLOYD: It looks
fundamentally different
because when you reduce
these to code or pseudo-code,
it's the only one that doesn't really
involve iterating over the array
multiple times, but at the expense
of this really sometimes painful
if you have to make a big shift
and do the sorting of people.
I personally when I'm teaching
in section, I start with bubble
and then I do selection.
And then I do--
DAVID MALAN: Yeah, that's
pretty much the ordering
I use unless things kind of--
I let things get out of
my control and just invite
students to propose
algorithms, which I then
try to slap a label on if it does
map to something that's traditionally
known or used in a class.
But what we're avoiding here,
notice, is merge sort comes later.
And we're not going to aspire to
something that's subquadratic until,
or like [? n log ?] n until
we're ready to get to that point.
But what is nice about selection,
bubble, and insertion too,
is that it lets us start to hint
at opportunities for optimization.
Like with bubble sort, you don't
have to just blindly go through
again and again n times over n elements.
You can actually short circuit
that if you notice, wait a minute.
I didn't do any swaps.
Let me stop then.
So even though we don't slap the
asymptotic notation on the discussion
just yet, there is an opportunity there.
DOUG LLOYD: To just see it.
DAVID MALAN: Exactly.
