Stepping Through Insertion Sort Function

WEBVTT
Kind: captions
Language: en

00:00:00.425 --> 00:00:02.050
What I want to do in
this video is step

00:00:02.050 --> 00:00:04.360
through the
insertion_sort function

00:00:04.360 --> 00:00:05.830
that we wrote in the last video.

00:00:05.830 --> 00:00:07.371
But before I do
that, I actually want

00:00:07.371 --> 00:00:10.490
to just focus on one part of it,
because I realize that I used

00:00:10.490 --> 00:00:13.084
something that you
probably don't recognize,

00:00:13.084 --> 00:00:14.500
because we haven't
used it before.

00:00:14.500 --> 00:00:17.850
I used the keyword
right over here.

00:00:17.850 --> 00:00:19.280
I used break.

00:00:19.280 --> 00:00:21.250
And you might guess
what break means,

00:00:21.250 --> 00:00:22.950
but now I'll explain
it explicitly.

00:00:22.950 --> 00:00:26.350
What break means is break
out of the smallest loop

00:00:26.350 --> 00:00:27.500
that you were doing.

00:00:27.500 --> 00:00:29.209
So we were taking
our item, and we

00:00:29.209 --> 00:00:31.250
kept comparing it to the
thing to the left of it.

00:00:31.250 --> 00:00:32.720
Or we were taking value
and comparing things

00:00:32.720 --> 00:00:33.810
to the left of it.

00:00:33.810 --> 00:00:36.737
And as soon as we found value
not being less than the thing

00:00:36.737 --> 00:00:38.570
to the left of it, we
said, hey, we're done.

00:00:38.570 --> 00:00:40.470
We don't have to keep
going to the left.

00:00:40.470 --> 00:00:42.950
And in that situation, I
want to break the loop.

00:00:42.950 --> 00:00:44.690
So we're breaking
this while loop.

00:00:44.690 --> 00:00:47.830
And then we just continue in our
next iteration of this for loop

00:00:47.830 --> 00:00:48.360
over here.

00:00:48.360 --> 00:00:49.457
So that's what break did.

00:00:49.457 --> 00:00:51.290
Now with that out of
the way, let's actually

00:00:51.290 --> 00:00:54.230
step through this program
on a simple example.

00:00:54.230 --> 00:00:57.250
So let's say that someone
calls-- well, let's just

00:00:57.250 --> 00:01:03.550
define a to be equal to-- let me
think of a fairly simple list--

00:01:03.550 --> 00:01:13.510
2, 1, 2, 1, I don't know, 2,
1, 3-- 2, 1, 3, 2, I think,

00:01:13.510 --> 00:01:15.900
is a good list over there.

00:01:15.900 --> 00:01:24.590
And let's assume that we are
calling insertion_sort on a.

00:01:24.590 --> 00:01:26.560
Let's think about
what's going to happen.

00:01:26.560 --> 00:01:28.980
Well, the first thing, of
course, that list-- let's

00:01:28.980 --> 00:01:32.010
keep track of all of
the variables here.

00:01:32.010 --> 00:01:34.210
So list is, right
from the get-go,

00:01:34.210 --> 00:01:39.160
going to refer to 2, 1, 3, 2.

00:01:39.160 --> 00:01:41.220
That's list right
from the get-go.

00:01:41.220 --> 00:01:44.430
And then we enter into the
function, for index in range.

00:01:44.430 --> 00:01:46.470
And let's parse this
part right over here.

00:01:46.470 --> 00:01:49.530
So what is len of the list?

00:01:49.530 --> 00:01:57.902
So len of our list is the same
thing as len of 2, 1, 3, 2.

00:01:57.902 --> 00:01:59.860
And this is just really
the number of elements.

00:01:59.860 --> 00:02:03.320
Len is short for length--
not that much shorter.

00:02:03.320 --> 00:02:06.740
But len of this is just
going to be 1, 2, 3, 4.

00:02:06.740 --> 00:02:09.130
It's going to be equal
to four elements.

00:02:09.130 --> 00:02:11.530
So this right over
here is going to be 4.

00:02:11.530 --> 00:02:19.320
And then the call, range
between 1 and len of list is 4.

00:02:19.320 --> 00:02:22.910
This will return the
list starting at 1,

00:02:22.910 --> 00:02:27.550
up to, but not
including 4, 1, 2, 3.

00:02:27.550 --> 00:02:29.820
And so these are the
indices we want to use,

00:02:29.820 --> 00:02:32.799
because the first index
is this right over here.

00:02:32.799 --> 00:02:34.340
Second index is this
right over here.

00:02:34.340 --> 00:02:35.990
And the third index
is that over here.

00:02:35.990 --> 00:02:38.570
Remember, this is
the 0-th index.

00:02:38.570 --> 00:02:41.390
So index is going to keep
incrementing between these two.

00:02:41.390 --> 00:02:44.250
It's going to be 1
first, then 3, then 2.

00:02:44.250 --> 00:02:49.040
So let me just create
our variable index

00:02:49.040 --> 00:02:52.080
whose scope is inside
of the for loop.

00:02:52.080 --> 00:02:54.840
So let's say we have index.

00:02:54.840 --> 00:02:56.890
Index is going to
start off being

00:02:56.890 --> 00:03:00.860
the first item in
this list right here,

00:03:00.860 --> 00:03:05.730
the list generated by
range, 1 comma len of list.

00:03:05.730 --> 00:03:09.260
So index is going to
start off being 1.

00:03:09.260 --> 00:03:10.850
And then over here,
we say value is

00:03:10.850 --> 00:03:14.430
equal to the index
element in list.

00:03:14.430 --> 00:03:18.850
So let me define
our variable value.

00:03:18.850 --> 00:03:22.502
So what is list of 1?

00:03:22.502 --> 00:03:24.210
So what is the first
element in the list?

00:03:24.210 --> 00:03:25.330
It's the 0-th element.

00:03:25.330 --> 00:03:27.250
This is the first element.

00:03:27.250 --> 00:03:30.290
So we're looking at
this right over here.

00:03:30.290 --> 00:03:33.300
It is the first element,
so that's fair enough.

00:03:33.300 --> 00:03:36.550
And then we define i
to be index minus 1.

00:03:36.550 --> 00:03:38.860
So let me put i over here.

00:03:38.860 --> 00:03:39.770
Do a new color.

00:03:39.770 --> 00:03:41.330
So now let's do i.

00:03:41.330 --> 00:03:42.930
It is index minus 1.

00:03:42.930 --> 00:03:45.840
Index is 1, so
index minus 1 is 0.

00:03:45.840 --> 00:03:50.580
So it's the index of the
item to the left of value.

00:03:50.580 --> 00:03:51.750
So that is going to be 0.

00:03:51.750 --> 00:03:54.370
This index is 1, this is 0.

00:03:54.370 --> 00:03:57.950
And then we're saying, while i
is greater than or equal to 0,

00:03:57.950 --> 00:03:59.474
do all of this
business over here.

00:03:59.474 --> 00:04:01.140
And the first thing
that we do over here

00:04:01.140 --> 00:04:05.510
is we compare value
to the object that

00:04:05.510 --> 00:04:08.030
is at the i-th
element in the list.

00:04:08.030 --> 00:04:09.400
So let me write that over here.

00:04:09.400 --> 00:04:14.640
So list at i.

00:04:14.640 --> 00:04:17.870
So that is going to be the
0-th element in the list,

00:04:17.870 --> 00:04:20.430
so that is 2.

00:04:20.430 --> 00:04:21.760
So we're comparing value.

00:04:21.760 --> 00:04:22.960
We're comparing 1.

00:04:22.960 --> 00:04:26.760
We're saying if 1 is less
than the i-th element in list,

00:04:26.760 --> 00:04:30.110
if 1 is less than
2, then do this.

00:04:30.110 --> 00:04:31.869
Well, 1 is definitely
less than 2.

00:04:31.869 --> 00:04:33.660
We're essentially taking
1 and comparing it

00:04:33.660 --> 00:04:34.960
to the thing to the
left and saying, hey,

00:04:34.960 --> 00:04:35.850
it's less than that.

00:04:35.850 --> 00:04:36.808
So it's less than that.

00:04:36.808 --> 00:04:39.115
So let's shift this
2 to the right,

00:04:39.115 --> 00:04:41.200
and let's shift 1 to the left.

00:04:41.200 --> 00:04:44.220
And so we go into here
and we say, list i plus 1.

00:04:44.220 --> 00:04:47.400
So what's i plus
1? i plus 1 is 1.

00:04:47.400 --> 00:04:51.490
So this is list of 1, so
this slot right over here.

00:04:51.490 --> 00:04:53.550
So this, in yellow
that I just underlined,

00:04:53.550 --> 00:04:56.320
that's this slot right
over here. i is 0.

00:04:56.320 --> 00:04:57.720
i plus 1 is 1.

00:04:57.720 --> 00:05:01.080
So the first item,
or the first slot,

00:05:01.080 --> 00:05:02.560
is this slot right over here.

00:05:02.560 --> 00:05:05.830
Let's replace it with
whatever is at list

00:05:05.830 --> 00:05:07.880
and whatever is in
the i-th element,

00:05:07.880 --> 00:05:11.590
or whatever is in the 0 slot,
I guess I should call it.

00:05:11.590 --> 00:05:13.560
So let's replace it with this 2.

00:05:13.560 --> 00:05:14.060
Right?

00:05:14.060 --> 00:05:14.934
Let me make it clear.

00:05:14.934 --> 00:05:17.460
And this time around,
this is 2, and this

00:05:17.460 --> 00:05:20.180
is the slot where the 1 was.

00:05:20.180 --> 00:05:21.880
So we're going to
put this-- we're

00:05:21.880 --> 00:05:24.200
going to replace that with 2.

00:05:24.200 --> 00:05:26.319
And then in the place
where the 2 was,

00:05:26.319 --> 00:05:27.860
we're going to
replace it with value.

00:05:27.860 --> 00:05:30.490
And remember,
value was set to 1.

00:05:30.490 --> 00:05:32.210
So value is set to 1.

00:05:32.210 --> 00:05:36.132
And so our list now
looks like 1, 2, 3, 2.

00:05:36.132 --> 00:05:37.590
And hopefully, this
looks familiar,

00:05:37.590 --> 00:05:39.560
if you remember when
we first described

00:05:39.560 --> 00:05:41.870
the insertion_sort algorithm.

00:05:41.870 --> 00:05:44.060
So we go through there.

00:05:44.060 --> 00:05:45.480
And now we want to decrement i.

00:05:45.480 --> 00:05:48.910
We say whatever i was--
it's 1-- we subtract 1.

00:05:48.910 --> 00:05:50.440
So now it's 0.

00:05:50.440 --> 00:05:53.473
So the new value for i is 0.

00:05:56.930 --> 00:05:57.760
Actually, no, no.

00:05:57.760 --> 00:05:58.260
Sorry.

00:05:58.260 --> 00:05:59.660
Whatever i was.

00:05:59.660 --> 00:06:02.100
i was 0.

00:06:02.100 --> 00:06:04.590
You subtract 1 from
that, and now i

00:06:04.590 --> 00:06:06.505
is going to be negative 1.

00:06:11.950 --> 00:06:13.340
And then we go to
the while loop.

00:06:13.340 --> 00:06:15.580
It says while i is greater
than or equal to 0.

00:06:15.580 --> 00:06:20.020
Well, i is now negative 1,
so the while loop no longer

00:06:20.020 --> 00:06:20.520
applies.

00:06:20.520 --> 00:06:23.490
This will return false. i is
not greater than or equal to 0,

00:06:23.490 --> 00:06:25.930
so it won't perform
any of this anymore.

00:06:25.930 --> 00:06:31.350
And so we'll now go to the
next iteration of the for loop.

00:06:31.350 --> 00:06:33.356
And essentially, what
that's signifying

00:06:33.356 --> 00:06:34.980
is that we were done
with that element.

00:06:34.980 --> 00:06:37.480
We compared all the way to
everything to the left of it,

00:06:37.480 --> 00:06:39.230
and it found its
place, or it just

00:06:39.230 --> 00:06:41.230
found its place in general.

00:06:41.230 --> 00:06:44.000
Now let's go to the next
iteration of the for loop.

00:06:44.000 --> 00:06:46.790
Now index is going to
be the next element.

00:06:46.790 --> 00:06:48.430
Now index, instead
of being 1, is

00:06:48.430 --> 00:06:51.260
going to be the next element
in the list generated

00:06:51.260 --> 00:06:52.810
by this expression here.

00:06:52.810 --> 00:06:55.160
So index is now going to be 2.

00:06:58.420 --> 00:07:03.960
And now list value is what's
ever at the second index.

00:07:03.960 --> 00:07:06.340
So it's this item
right over here.

00:07:06.340 --> 00:07:08.430
Notice, we were at the
second to the left.

00:07:08.430 --> 00:07:11.280
Now we're at the-- or we were
at the first to the left,

00:07:11.280 --> 00:07:13.220
or one right to the left of it.

00:07:13.220 --> 00:07:15.560
Now we're one space
more to the right.

00:07:15.560 --> 00:07:20.260
So value now is going to be 3.

00:07:20.260 --> 00:07:21.950
i is going to be index minus 1.

00:07:21.950 --> 00:07:23.900
Index is 2.

00:07:23.900 --> 00:07:28.090
2 minus 1 is 1.

00:07:28.090 --> 00:07:30.180
So this is now 1.

00:07:33.900 --> 00:07:36.860
And we say, while i is
greater than or equal to 0.

00:07:36.860 --> 00:07:40.900
Well, it's clearly greater
than or equal to 0 now.

00:07:40.900 --> 00:07:44.350
So if value is less than
list, the i-th element

00:07:44.350 --> 00:07:46.260
in list, so the value is 3.

00:07:46.260 --> 00:07:49.190
And is that less than--
well, what's at list i

00:07:49.190 --> 00:07:52.460
is no longer 2-- so what's
in the first element?

00:07:52.460 --> 00:07:55.760
Well, actually, it is still a 2.

00:07:55.760 --> 00:07:58.250
So if we look at the
first element-- list

00:07:58.250 --> 00:08:01.780
the i-th element in
list, i is 1, 0, 1.

00:08:01.780 --> 00:08:04.500
It is still 2, so I actually
didn't have to cross that out.

00:08:04.500 --> 00:08:08.740
It is still going to
be 2 right over there.

00:08:08.740 --> 00:08:13.060
And so if 3 is less
than 2, do this.

00:08:13.060 --> 00:08:15.000
Well, 3 isn't less
than 2, so you're

00:08:15.000 --> 00:08:16.340
going to do the else clause.

00:08:16.340 --> 00:08:19.320
And you're just going to
break out of this while loop.

00:08:19.320 --> 00:08:21.290
And that made sense,
because you said, look,

00:08:21.290 --> 00:08:23.782
we compared 3 immediately
to the thing right

00:08:23.782 --> 00:08:24.990
before it, to the left of it.

00:08:24.990 --> 00:08:27.160
And we said, hey, 3
is in the right place.

00:08:27.160 --> 00:08:28.180
3 is greater than 2.

00:08:28.180 --> 00:08:30.017
It's not less than
2, so I'm not going

00:08:30.017 --> 00:08:31.350
to do all the shifting business.

00:08:31.350 --> 00:08:33.699
I don't have to shift 2 to
the right and 3 to the left

00:08:33.699 --> 00:08:34.990
and then look at the next item.

00:08:34.990 --> 00:08:37.600
I know that everything to the
left of 3 is already sorted.

00:08:37.600 --> 00:08:39.750
So if 3 is not less than
2, it's definitely not

00:08:39.750 --> 00:08:42.150
going to be less than
anything to the left of it.

00:08:42.150 --> 00:08:42.900
And so we're done.

00:08:42.900 --> 00:08:44.810
We leave it the way it is.

00:08:44.810 --> 00:08:46.220
And so then we break out.

00:08:46.220 --> 00:08:49.000
And then we go to the next
iteration of the for loop.

00:08:49.000 --> 00:08:51.330
So now index is going to
be the next item here.

00:08:51.330 --> 00:08:53.710
It is now going
to be equal to 3,

00:08:53.710 --> 00:08:56.960
the next item in this list
generated by this expression.

00:08:56.960 --> 00:08:59.110
So it's now going to be 3.

00:08:59.110 --> 00:09:01.940
Index is now going to be 3.

00:09:01.940 --> 00:09:07.800
Value is now going
to be 0, 1, 2, 3.

00:09:07.800 --> 00:09:11.600
Value is now going to be
this item right over here.

00:09:11.600 --> 00:09:14.040
That is going to be value,
because our index is now 3.

00:09:14.040 --> 00:09:16.065
So our value is now 2.

00:09:16.065 --> 00:09:18.622
Let me cross these guys out.

00:09:18.622 --> 00:09:20.410
Let me save some space.

00:09:20.410 --> 00:09:22.570
Value is now 2.

00:09:22.570 --> 00:09:27.120
And now i is 2.

00:09:27.120 --> 00:09:31.640
i is index minus
1, so 3 minus 1.

00:09:31.640 --> 00:09:33.070
So that is 2.

00:09:33.070 --> 00:09:35.690
So i is now going to be 2.

00:09:35.690 --> 00:09:40.870
And the object that is at the
second element of the list, 0,

00:09:40.870 --> 00:09:45.310
1, 2 is now going to be 3.

00:09:45.310 --> 00:09:46.944
And so the first
thing we do while i

00:09:46.944 --> 00:09:48.485
is greater than or
equal to 0-- well,

00:09:48.485 --> 00:09:50.151
i is clearly greater
than or equal to 0.

00:09:50.151 --> 00:09:51.460
It's 2 right now.

00:09:51.460 --> 00:09:56.910
If value-- if 2 is
less than what's

00:09:56.910 --> 00:10:00.180
in the i-th slot of list,
so 2 is less than 3,

00:10:00.180 --> 00:10:02.810
so we do perform
this right over here.

00:10:02.810 --> 00:10:08.780
And so what we do is, whatever
is at list of i plus 1.

00:10:08.780 --> 00:10:10.660
I is 2. i plus 1 is 3.

00:10:10.660 --> 00:10:12.640
So whatever is at
this slot, which

00:10:12.640 --> 00:10:15.890
is the third slot where the 2
was, replace it with whatever

00:10:15.890 --> 00:10:17.090
is to the left of it.

00:10:17.090 --> 00:10:19.180
So replace it with the 3.

00:10:19.180 --> 00:10:21.140
So we're going to
replace this with the 3.

00:10:21.140 --> 00:10:22.990
And in the next
element, take whatever

00:10:22.990 --> 00:10:30.610
was that slot to the left
where i is, and replace it

00:10:30.610 --> 00:10:31.340
with value.

00:10:34.400 --> 00:10:36.060
So then we replaced
it with value.

00:10:36.060 --> 00:10:41.210
So this thing is now
going to be a 2 again.

00:10:41.210 --> 00:10:43.170
And now we decrement i again.

00:10:43.170 --> 00:10:46.290
So now i is equal to 1.

00:10:46.290 --> 00:10:48.020
And we go through
the while loop again.

00:10:48.020 --> 00:10:50.360
I is clearly greater
than or equal to 0.

00:10:50.360 --> 00:10:51.550
It is 1.

00:10:51.550 --> 00:10:53.500
Value is still 2.

00:10:53.500 --> 00:10:55.420
Remember 2, is what
we're comparing

00:10:55.420 --> 00:10:57.830
while we go through
this while loop,

00:10:57.830 --> 00:11:00.180
but we're comparing to each
of the items to the left.

00:11:00.180 --> 00:11:04.760
If 2 is less than whatever is
at the i-th slot in the list.

00:11:04.760 --> 00:11:06.970
So the first slot
in the list is a 2.

00:11:06.970 --> 00:11:09.940
Well, 2 is equal to that,
but it's not less than that,

00:11:09.940 --> 00:11:11.490
so we don't have to
do anything more.

00:11:11.490 --> 00:11:12.350
The thing is sorted.

00:11:12.350 --> 00:11:13.910
2 is in the right place.

00:11:13.910 --> 00:11:16.580
If it's equal to the
thing to the left of it,

00:11:16.580 --> 00:11:18.190
we don't have to shift anything.

00:11:18.190 --> 00:11:20.520
And that means it's at
least equal or greater

00:11:20.520 --> 00:11:22.060
than everything to
the left of that,

00:11:22.060 --> 00:11:25.920
since everything
is sorted already.

00:11:25.920 --> 00:11:27.420
So then we're done.

00:11:27.420 --> 00:11:31.590
Since 2 is not less
than 2, we break out.

00:11:31.590 --> 00:11:33.164
And we break out
in the for loop.

00:11:33.164 --> 00:11:34.580
It says, OK, let
me see if there's

00:11:34.580 --> 00:11:37.700
anything left in this
list to apply index to.

00:11:37.700 --> 00:11:38.300
There isn't.

00:11:38.300 --> 00:11:40.060
We've used everything
in that list.

00:11:40.060 --> 00:11:41.560
And so we break out
of the for loop.

00:11:41.560 --> 00:11:42.170
And we're done.

00:11:42.170 --> 00:11:43.420
And the list should be sorted.

00:11:43.420 --> 00:11:46.270
And we see right here,
the list is sorted.

00:11:46.270 --> 00:11:54.180
Our final sorted list
is 1, 2, 2, and 3.


Insertion Sort in Python

WEBVTT
Kind: captions
Language: en

00:00:00.259 --> 00:00:01.675
What I'm going to
do in this video

00:00:01.675 --> 00:00:05.480
is attempt to create an
implementation of the insertion

00:00:05.480 --> 00:00:08.145
sort algorithm that we talked
about in the last video,

00:00:08.145 --> 00:00:10.090
and I'll do it as
a Python function.

00:00:10.090 --> 00:00:15.420
So I'll call the Python
function insertion sort,

00:00:15.420 --> 00:00:17.790
and it will take in a list.

00:00:17.790 --> 00:00:20.530
So the list is its parameter
in the function definition.

00:00:20.530 --> 00:00:23.100
Someone would have to
pass list as an argument.

00:00:23.100 --> 00:00:26.200
And let's-- what we're going
to do is we're going to step

00:00:26.200 --> 00:00:29.934
through each of these
slots in the list.

00:00:29.934 --> 00:00:31.350
I guess we could
call it that way.

00:00:31.350 --> 00:00:35.960
So let's say 4
index in and range.

00:00:35.960 --> 00:00:38.980
We could start at the
leftmost slot in the list.

00:00:38.980 --> 00:00:41.580
So we could just
say len-- len just

00:00:41.580 --> 00:00:44.930
means it's short for length--
so length of the list.

00:00:44.930 --> 00:00:47.954
So what this would do,
this would start index.

00:00:47.954 --> 00:00:49.870
So let's say the list
has four elements in it.

00:00:49.870 --> 00:00:53.370
Then len list, this
right here would be 4,

00:00:53.370 --> 00:00:54.620
would evaluate to 4.

00:00:54.620 --> 00:00:57.310
Range of 4 would
produce a list that

00:00:57.310 --> 00:01:00.340
has the elements
0, 1, 2, 3 in it.

00:01:00.340 --> 00:01:01.940
And so index would
be able to step

00:01:01.940 --> 00:01:06.060
through the different indices
for this list right over here.

00:01:06.060 --> 00:01:07.560
And we could do it
that way, but you

00:01:07.560 --> 00:01:11.350
might remember from
the previous video

00:01:11.350 --> 00:01:13.596
that when you're doing
the insertion sort,

00:01:13.596 --> 00:01:15.220
it doesn't actually
make sense to start

00:01:15.220 --> 00:01:17.490
at the very leftmost
element, because there's

00:01:17.490 --> 00:01:19.850
nothing to compare
it to to the left.

00:01:19.850 --> 00:01:22.640
So we can actually just start
at the second to the leftmost

00:01:22.640 --> 00:01:23.360
element.

00:01:23.360 --> 00:01:26.010
And the leftmost element
is the 0-th item,

00:01:26.010 --> 00:01:28.150
so we can start
at the first item.

00:01:28.150 --> 00:01:30.680
So now if the list
has a length of 4,

00:01:30.680 --> 00:01:32.930
this right here will
produce 1, 2, 3.

00:01:32.930 --> 00:01:35.640
So it would-- 1 would be the
second to the leftmost element,

00:01:35.640 --> 00:01:38.700
2 would be the next one to the
right, 3 would be the last one.

00:01:38.700 --> 00:01:42.250
Remember, we always
start our indices at 0.

00:01:42.250 --> 00:01:46.170
The 0-th term is the
leftmost element in a list.

00:01:46.170 --> 00:01:47.070
So fine.

00:01:47.070 --> 00:01:48.520
We can step through that.

00:01:48.520 --> 00:01:50.780
And let's get the
value of right--

00:01:50.780 --> 00:01:55.690
at that point in time of the
element that is at that index.

00:01:55.690 --> 00:01:58.240
So that way we don't
have to keep finding it.

00:01:58.240 --> 00:02:01.969
Value is equal to list index.

00:02:01.969 --> 00:02:03.510
And this, by no
means, is going to be

00:02:03.510 --> 00:02:05.540
the most efficient
implementation of even

00:02:05.540 --> 00:02:06.810
insertion sort.

00:02:06.810 --> 00:02:09.860
It's just going to be my best
try writing it in real time

00:02:09.860 --> 00:02:13.440
and in a way that hopefully you
might be able to understand it.

00:02:13.440 --> 00:02:17.170
So value is just the item in the
list at each of those indexes

00:02:17.170 --> 00:02:19.360
that we're now going to
compare to all of the items

00:02:19.360 --> 00:02:21.350
to the left of it.

00:02:21.350 --> 00:02:29.190
And what I want to do is
compare value to each item

00:02:29.190 --> 00:02:30.650
to the left of it.

00:02:30.650 --> 00:02:33.590
So let's define a variable i.

00:02:33.590 --> 00:02:36.220
Let's let this be the
index of the things

00:02:36.220 --> 00:02:38.500
that I want to compare value to.

00:02:38.500 --> 00:02:40.080
And right from
the get-go, I want

00:02:40.080 --> 00:02:42.610
to compare value to the
thing that is left of it.

00:02:42.610 --> 00:02:47.020
So i should have one lower
of an index than index,

00:02:47.020 --> 00:02:49.990
so index minus 1.

00:02:49.990 --> 00:02:53.380
So this is the index of the item
that is directly left to it.

00:02:53.380 --> 00:02:55.490
But we're going to keep
taking i lower and lower,

00:02:55.490 --> 00:02:58.970
so we can keep comparing value
to things further and further

00:02:58.970 --> 00:03:00.254
to the left.

00:03:00.254 --> 00:03:01.670
And so what we
want to do is we're

00:03:01.670 --> 00:03:05.570
going to want to keep
comparing further and further

00:03:05.570 --> 00:03:07.500
left until i has
gotten all the way

00:03:07.500 --> 00:03:09.495
to the beginning of the list.

00:03:09.495 --> 00:03:11.370
And i has gotten all
the way to the beginning

00:03:11.370 --> 00:03:13.930
of the list when
it is equal to 0.

00:03:13.930 --> 00:03:16.220
So what we want to do is
we want to perform this

00:03:16.220 --> 00:03:20.129
while i is greater
than or equal to 0,

00:03:20.129 --> 00:03:22.420
because if we keep taking i
lower and lower and lower--

00:03:22.420 --> 00:03:24.880
we're going further and further
to the left in the list--

00:03:24.880 --> 00:03:27.720
we don't want to try to
perform it when i is further,

00:03:27.720 --> 00:03:28.940
is a negative number.

00:03:28.940 --> 00:03:31.250
That'll just start
doing crazy things.

00:03:31.250 --> 00:03:34.255
So while i is greater
than or equal to 0,

00:03:34.255 --> 00:03:35.630
what I'm going to
do is I'm going

00:03:35.630 --> 00:03:40.599
to keep pushing i further
and further to the left.

00:03:40.599 --> 00:03:41.640
So let's try it this way.

00:03:41.640 --> 00:03:42.600
So the first thing
I want to do-- we've

00:03:42.600 --> 00:03:44.610
already pushed it
to the left once.

00:03:44.610 --> 00:03:47.430
So let's compare.

00:03:47.430 --> 00:03:54.350
So if value is less
than the-- this thing

00:03:54.350 --> 00:03:55.975
keeps saying syntax
error, because it's

00:03:55.975 --> 00:03:57.730
waiting for me to keep typing.

00:03:57.730 --> 00:04:03.650
If value is less than the
item that is now at index i,

00:04:03.650 --> 00:04:08.450
so the item at index i,
list, the item at index i

00:04:08.450 --> 00:04:10.030
is this right here.

00:04:10.030 --> 00:04:12.620
So if it is less
than that, let's

00:04:12.620 --> 00:04:15.170
shift the item that's over here.

00:04:15.170 --> 00:04:17.600
Let's shift it one to the right.

00:04:17.600 --> 00:04:20.579
So the right slot is i plus 1.

00:04:20.579 --> 00:04:23.090
And I can't just say it's
index, because remember,

00:04:23.090 --> 00:04:25.520
I'm going to keep pulling i
in lower and lower and lower.

00:04:25.520 --> 00:04:27.510
Because right now, index is i.

00:04:27.510 --> 00:04:29.880
Right now i is index
minus 1 in the first pass

00:04:29.880 --> 00:04:31.480
through this while
loop, but I'm going

00:04:31.480 --> 00:04:34.021
to-- as you'll see in a second,
I'm going to keep lowering i.

00:04:34.021 --> 00:04:37.440
So it'll always won't
be one left of index.

00:04:37.440 --> 00:04:40.600
So I'm going to say wherever
i is, let's take the spot

00:04:40.600 --> 00:04:42.110
one to the right of i.

00:04:45.120 --> 00:04:47.510
So that's i plus 1 as its index.

00:04:47.510 --> 00:04:51.990
And let's replace that
with whatever's at list,

00:04:51.990 --> 00:04:54.737
or whatever is at i, at slot i.

00:04:54.737 --> 00:04:57.070
So we have essentially taken
this thing right over here,

00:04:57.070 --> 00:05:00.210
whatever number was there,
and we are putting it

00:05:00.210 --> 00:05:02.803
in the slot that is
one to the right of it.

00:05:02.803 --> 00:05:05.010
And then-- and actually
the way we're setting up

00:05:05.010 --> 00:05:08.030
that algorithm, whatever's there
is actually going to be-- well,

00:05:08.030 --> 00:05:09.240
I won't talk about that.

00:05:09.240 --> 00:05:12.270
We'll step through it and
see how it all plays out.

00:05:12.270 --> 00:05:16.450
And then we can shift
value to the left.

00:05:16.450 --> 00:05:19.090
So whatever was in this
slot right over here

00:05:19.090 --> 00:05:21.310
will be replaced by value.

00:05:21.310 --> 00:05:26.550
So list i will equal value.

00:05:26.550 --> 00:05:29.080
So one way to think about it--
let me write a comment here.

00:05:29.080 --> 00:05:42.546
Shift what was-- shift number
in slot i to slot i plus 1.

00:05:42.546 --> 00:05:43.990
Or you can say
bucket i plus 1, I

00:05:43.990 --> 00:05:46.120
guess is one way
to think about it.

00:05:46.120 --> 00:05:50.190
And then you could say
shift number right.

00:05:50.190 --> 00:05:51.240
Let me call it this way.

00:05:51.240 --> 00:05:53.189
Shift number right in slot.

00:05:53.189 --> 00:05:54.230
Let me write it this way.

00:05:54.230 --> 00:06:00.260
Shift number in slot i
right to slot i plus 1.

00:06:00.260 --> 00:06:05.520
And then over here,
we are shifting--

00:06:05.520 --> 00:06:13.312
shift value left into slot i.

00:06:13.312 --> 00:06:15.520
And so if you remember what
we did in the last video,

00:06:15.520 --> 00:06:17.030
this is exactly describing it.

00:06:17.030 --> 00:06:19.430
We're comparing value to
the thing to the left of it.

00:06:19.430 --> 00:06:22.444
If it's less than it, then
whatever number was in that box

00:06:22.444 --> 00:06:24.110
to the left of it,
the slot to the left,

00:06:24.110 --> 00:06:27.720
shift it to the right, and
then shift value to the left.

00:06:27.720 --> 00:06:30.990
And now compare value to
something 1 lower than that.

00:06:30.990 --> 00:06:32.560
So we want to decrement i.

00:06:32.560 --> 00:06:33.640
We want to lower i.

00:06:33.640 --> 00:06:36.120
Decrement is just
increment down.

00:06:36.120 --> 00:06:41.010
So i is equal to i minus 1.

00:06:41.010 --> 00:06:44.591
And then we'll perform
the loop over again.

00:06:44.591 --> 00:06:46.090
And now value will
be compared-- now

00:06:46.090 --> 00:06:48.470
i is two to the left of index.

00:06:48.470 --> 00:06:49.590
Compare it to that.

00:06:49.590 --> 00:06:51.590
If it is less than it,
shift that to the right

00:06:51.590 --> 00:06:54.150
and shift value
again to the left.

00:06:54.150 --> 00:06:58.160
Now, what if we have the
situation where value is not

00:06:58.160 --> 00:07:00.510
less than the item that
you're comparing it to?

00:07:00.510 --> 00:07:02.150
Well, if it's not
less than the item

00:07:02.150 --> 00:07:04.900
that you're comparing it to
and that that means that value

00:07:04.900 --> 00:07:07.034
is already going to
be in the right place,

00:07:07.034 --> 00:07:08.950
it also means that you're
done, that you don't

00:07:08.950 --> 00:07:10.880
need to shift value
any more to the left,

00:07:10.880 --> 00:07:12.713
and you don't have to
shift any of the stuff

00:07:12.713 --> 00:07:14.330
to the left any
more to the right.

00:07:14.330 --> 00:07:18.290
So then we are done.

00:07:18.290 --> 00:07:20.790
And I think this
could work unless I've

00:07:20.790 --> 00:07:22.670
made some silly mistake.

00:07:22.670 --> 00:07:24.750
So let's try to see
if I can get this--

00:07:24.750 --> 00:07:28.250
if this actually works
as a sorting algorithm.

00:07:28.250 --> 00:07:32.390
So let me save it-- insertion
sort-- and let me run it.

00:07:32.390 --> 00:07:32.940
All right.

00:07:32.940 --> 00:07:35.260
So I didn't have any at
least syntax mistakes.

00:07:35.260 --> 00:07:38.320
Syntax just means the
actual characters I use.

00:07:38.320 --> 00:07:41.380
I didn't forget to put a colon
here or a greater than sign.

00:07:41.380 --> 00:07:45.250
It was actually able to process
this, interpret this right over

00:07:45.250 --> 00:07:45.750
here.

00:07:45.750 --> 00:07:47.500
But let's see if
it actually works.

00:07:47.500 --> 00:07:50.610
So let me define a list.

00:07:50.610 --> 00:07:59.360
Let's say it is
7, 1, 3, 5, 9, 2.

00:07:59.360 --> 00:08:01.350
And let me put
another 3 in there.

00:08:01.350 --> 00:08:03.750
So that is a, and
then let me see.

00:08:03.750 --> 00:08:05.420
This is the moment of truth.

00:08:05.420 --> 00:08:10.610
Insertion sort of a--
let's see what happens.

00:08:10.610 --> 00:08:12.640
So remember, we're
sorting it in place.

00:08:12.640 --> 00:08:14.310
This function doesn't
return anything,

00:08:14.310 --> 00:08:17.030
but it should take whatever
list that was and had changed up

00:08:17.030 --> 00:08:19.330
all of the elements so
that now they are in order.

00:08:19.330 --> 00:08:21.040
So this is the moment of truth.

00:08:21.040 --> 00:08:23.640
Let's see what a looks like.

00:08:23.640 --> 00:08:24.480
There you go.

00:08:24.480 --> 00:08:25.320
It is sorted.

00:08:25.320 --> 00:08:28.500
So I don't think I made
any major mistakes.

00:08:28.500 --> 00:08:29.900
So there you go.

00:08:29.900 --> 00:08:35.074
You have insertion-- a
version of insertion sort.


Insertion Sort Algorithm

WEBVTT
Kind: captions
Language: en

00:00:00.264 --> 00:00:01.680
What I want to do
in this video is

00:00:01.680 --> 00:00:04.090
go over what I think is one
of the more intuitive ways

00:00:04.090 --> 00:00:04.820
to sort a list.

00:00:04.820 --> 00:00:07.810
It's how I would probably sort
it, if I had to do it manually.

00:00:07.810 --> 00:00:10.410
But I want to make it clear, it
is not the most efficient way

00:00:10.410 --> 00:00:12.140
to sort a list.

00:00:12.140 --> 00:00:15.060
I think it's a good starting
point to getting warmed up

00:00:15.060 --> 00:00:15.880
with sorting lists.

00:00:15.880 --> 00:00:17.005
It's called insertion_sort.

00:00:21.570 --> 00:00:24.320
And I'm going to give
a graphical description

00:00:24.320 --> 00:00:26.600
of the algorithm
for insertion_sort.

00:00:26.600 --> 00:00:28.100
And just so you
know what, algorithm

00:00:28.100 --> 00:00:30.780
sounds like a very fancy
word, but an algorithm

00:00:30.780 --> 00:00:35.120
is really just a way of sorting
it, or a process for doing it,

00:00:35.120 --> 00:00:36.560
or a method for doing it.

00:00:36.560 --> 00:00:40.430
A program is a specific
implementation of an algorithm,

00:00:40.430 --> 00:00:43.520
or it can be a specific
implementation of an algorithm.

00:00:43.520 --> 00:00:46.510
Once I have a general algorithm,
I could implement it in Python.

00:00:46.510 --> 00:00:49.310
I could implement it in C. I
could implement it in Java.

00:00:49.310 --> 00:00:50.790
Those are specific programs.

00:00:50.790 --> 00:00:53.931
But they'd all be implementing
the same way of doing the sort.

00:00:53.931 --> 00:00:55.430
And that's what I'm
describing here,

00:00:55.430 --> 00:00:58.220
the insertion_sort algorithm.

00:00:58.220 --> 00:01:00.540
So let's just start
with a simple example.

00:01:00.540 --> 00:01:02.660
Let's say that I have a list.

00:01:02.660 --> 00:01:04.610
Let's say I have a Python
list, because we're

00:01:04.610 --> 00:01:06.680
going to be working
in Python for this.

00:01:06.680 --> 00:01:16.020
And that list is, let's
say, it is 7, 3, 1, 2, 4, 6.

00:01:16.020 --> 00:01:19.950
And so the way that
we do insertion_sort

00:01:19.950 --> 00:01:22.470
is you go element by element.

00:01:22.470 --> 00:01:25.140
And then you compare it
to the elements before it.

00:01:25.140 --> 00:01:28.080
And then you look for the
first element before it

00:01:28.080 --> 00:01:30.230
that it is actually less than.

00:01:30.230 --> 00:01:32.340
And then you just stick
it right over there.

00:01:32.340 --> 00:01:34.630
So let me show you
what I'm talking about.

00:01:34.630 --> 00:01:40.496
So you could start with the
7, the 0-th element over here.

00:01:40.496 --> 00:01:42.120
But when you start
with a 0-th element,

00:01:42.120 --> 00:01:43.200
you're like, hey,
wait, there's nothing

00:01:43.200 --> 00:01:44.396
before it to compare it to.

00:01:44.396 --> 00:01:47.020
So it really doesn't make sense
to start with the 0-th element.

00:01:47.020 --> 00:01:49.300
So really, if you were to
implement this algorithm,

00:01:49.300 --> 00:01:50.960
you'll start it with
the third element.

00:01:50.960 --> 00:01:54.090
Or sorry, you'll start it with--
if this is the 0-th element,

00:01:54.090 --> 00:01:57.440
you'd start it with the first
element right over here.

00:01:57.440 --> 00:01:58.790
If this is 0-th, this is first.

00:01:58.790 --> 00:02:00.010
I know this can be a
little confusing when

00:02:00.010 --> 00:02:01.750
you refer to this as
the first element.

00:02:01.750 --> 00:02:03.150
But this one's the 0-th.

00:02:03.150 --> 00:02:05.170
So you start with this 3 here.

00:02:05.170 --> 00:02:07.570
And then you start comparing
it to all of the elements

00:02:07.570 --> 00:02:08.490
before it.

00:02:08.490 --> 00:02:12.040
And as soon as you find an
element that 3 is less than,

00:02:12.040 --> 00:02:14.370
you stick it in that
part of the list.

00:02:14.370 --> 00:02:18.210
So what you do is you
say, OK, is 3 less than 7?

00:02:18.210 --> 00:02:20.530
Well, yeah, it is less than 7.

00:02:20.530 --> 00:02:25.740
So what you want to do is you
want to shift 7 where the 3 is.

00:02:25.740 --> 00:02:26.920
So let me put it out here.

00:02:26.920 --> 00:02:30.940
So we're trying to compare 3 to
everything before it right now.

00:02:34.580 --> 00:02:36.240
So you say, OK,
is 3 less than 7?

00:02:36.240 --> 00:02:37.700
Yeah, it is less than 7.

00:02:37.700 --> 00:02:39.840
So let's put the
7 where the 3 is.

00:02:42.570 --> 00:02:47.740
And let's put the 3 where
the 7 is, especially

00:02:47.740 --> 00:02:51.620
because there's nothing
left to compare the 3 to.

00:02:51.620 --> 00:02:54.180
There's no elements
before the 0-th element,

00:02:54.180 --> 00:02:57.050
so let's just stick
the 3 right over there.

00:02:57.050 --> 00:02:59.340
And so our list now
looks like this.

00:02:59.340 --> 00:03:07.510
Our list now looks like
3, 7, 1, 2, 4, and 6.

00:03:07.510 --> 00:03:09.406
And one thing you'll
find interesting,

00:03:09.406 --> 00:03:10.780
or something to
pay attention to,

00:03:10.780 --> 00:03:14.525
is as we build this list--
so the 0-th element is now

00:03:14.525 --> 00:03:16.150
definitely less than
the first element.

00:03:16.150 --> 00:03:18.520
And everything up to and
including the first element

00:03:18.520 --> 00:03:19.435
is now sorted.

00:03:22.840 --> 00:03:25.930
And that will be true, as
we keep going through this.

00:03:25.930 --> 00:03:28.400
As we keep going through higher
and higher indexes, up to

00:03:28.400 --> 00:03:30.400
and including that
index after we've done

00:03:30.400 --> 00:03:32.290
that pass will be sorted.

00:03:32.290 --> 00:03:34.530
And I'll try to point
that out as we go along.

00:03:34.530 --> 00:03:36.250
So we did the first index.

00:03:36.250 --> 00:03:37.800
So we already did
the first index.

00:03:37.800 --> 00:03:41.070
Now we can move on to
the second element,

00:03:41.070 --> 00:03:43.190
which is this 1 over here.

00:03:43.190 --> 00:03:44.800
And so you take that 1.

00:03:44.800 --> 00:03:46.914
I'll put it on the
side right over here.

00:03:46.914 --> 00:03:49.330
You take that 1 and you compare
it to each of the elements

00:03:49.330 --> 00:03:50.070
before it.

00:03:50.070 --> 00:03:51.640
OK, is 1 less than 7?

00:03:51.640 --> 00:03:54.150
Sure, 1 is less than 7.

00:03:54.150 --> 00:03:56.720
So let's stick the
7 where the 1 is.

00:03:59.410 --> 00:04:02.950
And then you could
just keep comparing,

00:04:02.950 --> 00:04:06.110
or you could just stick
the 1 where the 7 is.

00:04:06.110 --> 00:04:08.680
And then you would say,
OK, is 1 less than 3?

00:04:08.680 --> 00:04:10.010
Well, yeah, sure.

00:04:10.010 --> 00:04:11.130
It's less than 3.

00:04:11.130 --> 00:04:15.000
So let's stick the
3 where the 1 is.

00:04:15.000 --> 00:04:18.839
And let's put the
1 where the 3 is.

00:04:18.839 --> 00:04:21.110
So what is our list now?

00:04:21.110 --> 00:04:32.500
Our list now is going to look
like 1, 1, 3, 7, 2, 4, 6.

00:04:32.500 --> 00:04:36.380
So notice, after we've
gotten to the n-th index-- so

00:04:36.380 --> 00:04:38.170
in this case, this
was the index of 2

00:04:38.170 --> 00:04:40.590
where that 1 was right
over there-- everything up

00:04:40.590 --> 00:04:42.660
to and including
that index is sorted.

00:04:42.660 --> 00:04:45.370
This part right here is sorted.

00:04:45.370 --> 00:04:47.620
Other things are going to
be thrown in here, probably,

00:04:47.620 --> 00:04:48.430
as we go on.

00:04:48.430 --> 00:04:50.150
But so far, this part is sorted.

00:04:50.150 --> 00:04:52.840
So you can see by the time we
get to the end of this list,

00:04:52.840 --> 00:04:55.160
everything is
going to be sorted.

00:04:55.160 --> 00:04:57.920
So let's now go
to the next index,

00:04:57.920 --> 00:05:00.240
or the next item in the list.

00:05:00.240 --> 00:05:02.110
And that is this 2 over here.

00:05:05.300 --> 00:05:07.560
And so we compare
the 2 to the 7.

00:05:07.560 --> 00:05:10.450
2 is definitely less
than the 7, so let's put

00:05:10.450 --> 00:05:14.000
the 7 where the 2 is, and
let's put the 2 where the 7 is.

00:05:14.000 --> 00:05:16.110
Now you compare the 2 to the 3.

00:05:16.110 --> 00:05:17.820
2 is less than 3.

00:05:17.820 --> 00:05:20.370
So let's put the 3 where
the 2 is, and let's

00:05:20.370 --> 00:05:22.220
put the 2 where the 3 is.

00:05:22.220 --> 00:05:24.230
Then compare 2 to 1.

00:05:24.230 --> 00:05:25.720
Is 2 less than 1?

00:05:25.720 --> 00:05:29.840
No, it is not less than 1, so we
don't have to do anything else.

00:05:29.840 --> 00:05:32.360
We don't have to keep
going to the left.

00:05:32.360 --> 00:05:34.170
And so now, after this
pass-- in this pass,

00:05:34.170 --> 00:05:37.830
we were comparing this 2 item
to everything before that.

00:05:37.830 --> 00:05:41.450
So after this pass, our
list looks like this.

00:05:41.450 --> 00:05:51.380
Our list looks like
1, 2, 3, 7, 4, 6.

00:05:51.380 --> 00:05:55.890
And notice everything up to
and including the third item,

00:05:55.890 --> 00:05:59.530
so 0-th 1 2, third
item, is now sorted.

00:05:59.530 --> 00:06:05.100
And now we're ready to look at
the next element in the list.

00:06:05.100 --> 00:06:06.640
And one thing I
want to make clear,

00:06:06.640 --> 00:06:08.265
when you actually
implement it, there's

00:06:08.265 --> 00:06:10.590
a couple of ways to do it.

00:06:10.590 --> 00:06:12.800
So in this example, we
said, hey, 2 is less than 7.

00:06:12.800 --> 00:06:15.836
We had the 7 replace where
the 2 is, which you should do.

00:06:15.836 --> 00:06:17.710
And then we had the 2
replace where the 7 is.

00:06:17.710 --> 00:06:20.130
But the reality is you
can keep going down,

00:06:20.130 --> 00:06:22.080
until you find a good
place to put the 2,

00:06:22.080 --> 00:06:24.470
and shifting everything
to the right as you do it,

00:06:24.470 --> 00:06:25.769
and then putting the 2 in.

00:06:25.769 --> 00:06:28.310
Although this way, it's a little
bit easier to keep track of.

00:06:28.310 --> 00:06:30.143
And well, maybe we'll
explore different ways

00:06:30.143 --> 00:06:32.530
to do it when we actually
implement the algorithm.

00:06:32.530 --> 00:06:33.970
Anyway, let's look at this 4.

00:06:33.970 --> 00:06:35.800
Same exact idea.

00:06:35.800 --> 00:06:38.240
4 is less than 7,
so let's put the 7

00:06:38.240 --> 00:06:41.226
where the 4 is and put
the 4 where the 7 is.

00:06:41.226 --> 00:06:42.280
Is 4 less than 3?

00:06:42.280 --> 00:06:44.990
No, it's not less
than 3, so we stop.

00:06:44.990 --> 00:06:45.790
We're done.

00:06:45.790 --> 00:06:49.490
Now everything up to and
including the fourth item

00:06:49.490 --> 00:06:53.230
in this list, 0, 1, 2,
3, 4, is now sorted.

00:06:53.230 --> 00:06:55.590
And now our list
looks like-- let

00:06:55.590 --> 00:07:00.345
me scroll down a little bit--
now our list looks like this.

00:07:00.345 --> 00:07:01.220
Let me write it down.

00:07:01.220 --> 00:07:08.710
It is 1, 2, 3, 4, 7, and then 6.

00:07:08.710 --> 00:07:13.216
And now we can go to this
last element in the list.

00:07:13.216 --> 00:07:14.840
This is the 6 that
we're now comparing.

00:07:14.840 --> 00:07:17.200
Actually, the last
time we did this,

00:07:17.200 --> 00:07:19.340
it was a 4 that we cared about.

00:07:19.340 --> 00:07:20.790
But now we care about the 6.

00:07:20.790 --> 00:07:22.380
Is 6 less than 7?

00:07:22.380 --> 00:07:23.070
Sure, it is.

00:07:23.070 --> 00:07:24.750
So let's put 7 where the 6 is.

00:07:24.750 --> 00:07:26.430
Let's put a 6 where the 7 is.

00:07:26.430 --> 00:07:28.030
Is 6 less than 4?

00:07:28.030 --> 00:07:29.250
No, it's not.

00:07:29.250 --> 00:07:30.970
And so we stop, because
we know that this

00:07:30.970 --> 00:07:31.820
is going to be sorted.

00:07:31.820 --> 00:07:33.236
If we went any
further, we're just

00:07:33.236 --> 00:07:35.660
going to get elements that
are less than or equal to 4.

00:07:35.660 --> 00:07:36.890
So we are done.

00:07:36.890 --> 00:07:38.750
We have sorted this list.

00:07:38.750 --> 00:07:43.459
The sorted list is now
1, 2, 3, 4, 6, and 7.

00:07:43.459 --> 00:07:45.000
In the next video,
I'm actually going

00:07:45.000 --> 00:07:47.230
to try to implement this
algorithm that I just

00:07:47.230 --> 00:07:48.150
described.

00:07:48.150 --> 00:07:51.860
And I'm going to implement it
in a simple Python function.


Exercise - Write a Sorting Function

WEBVTT
Kind: captions
Language: en

00:00:00.500 --> 00:00:03.660
What I want you to attempt to
do is given an arbitrary list--

00:00:03.660 --> 00:00:06.030
I made a particular example
of an arbitrary list right

00:00:06.030 --> 00:00:09.880
here-- write a function that
sorts that arbitrary list.

00:00:09.880 --> 00:00:12.340
So maybe you call
your function My Sort,

00:00:12.340 --> 00:00:13.670
you can call it whatever.

00:00:13.670 --> 00:00:19.800
What I want is after I call
My Sort on a list, the list

00:00:19.800 --> 00:00:21.130
itself gets resorted.

00:00:21.130 --> 00:00:25.080
So I don't want My Sort to
return a copy of the list.

00:00:25.080 --> 00:00:29.710
I want, after I call My Sort
on a, if I then refer to a,

00:00:29.710 --> 00:00:33.920
if let's say I do print a,
then that will literally

00:00:33.920 --> 00:00:36.270
return a sorted version of a.

00:00:36.270 --> 00:00:40.110
Or I should say it will return
a, but now a itself is sorted,

00:00:40.110 --> 00:00:43.090
or the list that a is
referring to is now sorted.

00:00:43.090 --> 00:00:45.590
So it would now
look like this. a

00:00:45.590 --> 00:00:51.380
would now look like
1, 2, 3, 5, 7, 8.

00:00:51.380 --> 00:00:53.510
And you might say, wait
Sal, that's very easy,

00:00:53.510 --> 00:00:55.690
there is already a sort
function in Python.

00:00:55.690 --> 00:00:59.770
You literally can
call a dot sort,

00:00:59.770 --> 00:01:02.750
and that will sort a
in place just the way

00:01:02.750 --> 00:01:03.921
I've asked you to do.

00:01:03.921 --> 00:01:05.379
And when I say in
place, I'm saying

00:01:05.379 --> 00:01:08.192
it's actually going to
work on the actual list.

00:01:08.192 --> 00:01:10.650
It's not going to create a copy
of the list that is sorted.

00:01:10.650 --> 00:01:14.420
It's actually going to work
in place on that actual list.

00:01:14.420 --> 00:01:17.550
And a sort does this, but I
don't want you to use that.

00:01:17.550 --> 00:01:18.780
So let's cross this off.

00:01:18.780 --> 00:01:21.460
The whole point of
this is to learn just

00:01:21.460 --> 00:01:25.255
to kind of try our best to
write our own sorting function.

00:01:25.255 --> 00:01:26.880
What you're going to
see, and hopefully

00:01:26.880 --> 00:01:29.200
appreciate as we go deeper
and deeper into this, is it

00:01:29.200 --> 00:01:32.320
there's many, many, many,
many ways to sort things.

00:01:32.320 --> 00:01:37.460
And sorting is really a good
way to think about how good

00:01:37.460 --> 00:01:40.700
a function, and we'll start
talking about algorithms,

00:01:40.700 --> 00:01:43.660
how good a process is,
or how fast a process is,

00:01:43.660 --> 00:01:47.090
or how efficient a process
is, at doing something.

00:01:47.090 --> 00:01:48.430
So I'll leave you there.

00:01:48.430 --> 00:01:49.890
Try to sort this.

00:01:49.890 --> 00:01:53.100
In the next video, I'll try
to outline an algorithm, which

00:01:53.100 --> 00:01:55.660
is really just a process
for doing something.

00:01:55.660 --> 00:01:58.040
I'll outline an
algorithm that's kind

00:01:58.040 --> 00:01:59.520
of an intuitive
way to sort things.

00:01:59.520 --> 00:02:01.478
It's not the most efficient
way to sort things.

00:02:01.478 --> 00:02:06.335
And then after that,
I'll try my best try

00:02:06.335 --> 00:02:11.272
at actually writing that
algorithm in a Python program.


Stepping Through Recursive Fibonacci Function

WEBVTT
Kind: captions
Language: en

00:00:00.933 --> 00:00:02.948
What I want to do in this video

00:00:02.948 --> 00:00:05.133
is make sure we really understand what is going on

00:00:05.133 --> 00:00:08.067
when we call our recursive fibonacci functions

00:00:08.067 --> 00:00:10.800
So I'm going to assume that someone calls it with

00:00:10.850 --> 00:00:16.138
an argument of and they give pass 5 as an argument

00:00:16.200 --> 00:00:18.733
I don't want to pick too large of a number

00:00:18.733 --> 00:00:21.267
cuz otherwise I'll be explaining it forever

00:00:21.267 --> 00:00:23.200
So let's try fibonacci(5)

00:00:23.200 --> 00:00:27.067
So in this situation, within the context of this function

00:00:27.067 --> 00:00:32.467
the parameter n right here is going to be equal to 5

00:00:32.467 --> 00:00:37.400
so in that first pass, the parameter n is going to be equal to 5

00:00:37.400 --> 00:00:39.313
The way we wrote it

00:00:39.313 --> 00:00:41.129
we said that if n < 2 return n

00:00:41.129 --> 00:00:42.867
5 is definitely not less than 2

00:00:42.867 --> 00:00:46.133
so we're going to go to the else part of the if clause

00:00:46.133 --> 00:00:49.138
or the else clause and say return

00:00:49.138 --> 00:00:53.100
fibonacci of (n-1) plus fibonnaci of (n-2))

00:00:53.100 --> 00:00:55.800
so when I call this it's eventually be reduced to

00:00:55.800 --> 00:00:58.656
if you want to think about it that way, or simplified

00:00:58.656 --> 00:01:07.005
it would return, what is going to be the same thing as fibonacci of

00:01:07.005 --> 00:01:09.585
remember n was 5

00:01:09.600 --> 00:01:14.488
so n-1 is 4

00:01:14.611 --> 00:01:20.395
plus fibonacci of n-2, which when we ran the function n was 5

00:01:20.395 --> 00:01:22.108
so 5-2 is 3

00:01:22.246 --> 00:01:24.595
well these are just more function calls

00:01:24.595 --> 00:01:26.729
so now we are going to go again

00:01:26.729 --> 00:01:28.718
n is not 5, but 4 and 3

00:01:28.718 --> 00:01:30.600
so let's try this out

00:01:30.600 --> 00:01:34.467
so over here n is equal to 4

00:01:34.467 --> 00:01:38.200
n is equal to 4

00:01:38.200 --> 00:01:40.600
so once again 4 is not less than 2,

00:01:40.600 --> 00:01:42.867
so we don't do this part

00:01:42.867 --> 00:01:44.552
we go to the else

00:01:44.552 --> 00:01:47.467
we then return fibbonacci(4-1) which is 3

00:01:47.467 --> 00:01:49.933
so this is going to simplify to,

00:01:49.933 --> 00:01:53.610
or breakdown I should say

00:01:53.610 --> 00:01:56.595
to fibonnacci of 4-1 which is 3

00:01:56.641 --> 00:02:03.982
plus fibbonacci of 4-2

00:02:04.120 --> 00:02:04.620
which is fibonaacci of 2

00:02:04.620 --> 00:02:09.467
so this right over here will essenitally return this

00:02:09.467 --> 00:02:14.467
and this over here on the right fibonacci of 3

00:02:14.467 --> 00:02:17.641
let me draw, because these are going to get jumbled up soon

00:02:17.641 --> 00:02:22.169
so this returns this stuff in magenta

00:02:22.169 --> 00:02:26.400
and this stuff I've underlined in green will return

00:02:26.400 --> 00:02:29.359
n is now 3; 3 is not less than 2

00:02:29.359 --> 00:02:32.346
so you go here

00:02:32.346 --> 00:02:42.133
and it will return fibonacci of 3-1 which is fibbonacci (2)

00:02:42.133 --> 00:02:51.467
and plus fibonacci of 3-2, which is fibonacci (1)

00:02:51.467 --> 00:02:53.523
and then we're going to go over here

00:02:53.523 --> 00:02:56.021
and we're going to have to calculate each of these things

00:02:56.021 --> 00:02:59.133
and these are just more calls to fibonacci

00:02:59.133 --> 00:03:02.667
and fibonacci(3), so you can see how this is getting pretty involved right now

00:03:02.667 --> 00:03:04.933
I'm going to start writing fib short for fibonacci

00:03:04.933 --> 00:03:07.800
so that I don't run out of real estate

00:03:07.800 --> 00:03:11.231
fibonacci (3) when you call it

00:03:11.231 --> 00:03:13.720
n 3 is not less than 2

00:03:13.720 --> 00:03:18.933
that reduces to fibonacci (3-1)

00:03:18.933 --> 00:03:21.267
I'll just write fib. short for fibonacci

00:03:21.267 --> 00:03:26.262
fibonacci of 2 plus fibbonacci of (3-2)

00:03:26.262 --> 00:03:29.282
plus fibonacci of 1

00:03:29.298 --> 00:03:32.662
so it reduces to that or breaks down to that

00:03:32.662 --> 00:03:35.933
and over here fibonacci of 2

00:03:35.933 --> 00:03:38.452
2 is not less than 2,

00:03:38.452 --> 00:03:42.434
so we are going to have to return fibonacci of 2 -1

00:03:42.434 --> 00:03:43.600
fibonacci of 1 plus fibonacci of 2-2

00:03:43.600 --> 00:03:49.933
so plus fibonacci of 0

00:03:49.933 --> 00:03:53.800
so it breaks down to those two calls to fibonacci

00:03:53.800 --> 00:03:55.933
and over here fibonacci(2) same thing.

00:03:56.025 --> 00:03:58.144
we made a call to fibonacci(2)

00:03:58.144 --> 00:04:01.080
that's going to break down just like that fibonacci(2) did

00:04:01.080 --> 00:04:03.323
it will break down to a call

00:04:03.323 --> 00:04:08.182
to of fibbonacci of 1 and fibonacci(0)

00:04:08.182 --> 00:04:09.686
and then we have fibonacci of 1

00:04:09.978 --> 00:04:11.374
so this is interesting.

00:04:11.374 --> 00:04:13.138
Because when n is equal to 1

00:04:13.138 --> 00:04:16.336
this clause up here suddenly becomes relevant

00:04:16.336 --> 00:04:20.025
Because n is less than 2 and it says return n

00:04:20.025 --> 00:04:22.800
so this, this right here is going to simplify

00:04:22.800 --> 00:04:25.920
this term right over here is going to simplify to 1

00:04:26.105 --> 00:04:28.569
it is going to evaluate to 1

00:04:28.569 --> 00:04:30.713
And then we look at all of these over here

00:04:30.713 --> 00:04:36.841
fibonacci (2); we know that fibonacci(2) results in fib(1) + fib(0)

00:04:36.841 --> 00:04:39.308
so let me write that over here

00:04:39.308 --> 00:04:44.052
so over here is fibbonacci(1) plus fibbonacci(0)

00:04:44.052 --> 00:04:46.302
fibb is short for fibonacci

00:04:46.302 --> 00:04:49.600
and then we know fibbonacci of 1

00:04:49.600 --> 00:04:52.133
1 is less than 2, so return n

00:04:52.133 --> 00:04:56.533
so this is going to return 1

00:04:56.533 --> 00:04:58.933
fibonacci of 1 just returns 1

00:04:58.933 --> 00:05:00.533
fibonacci of 0

00:05:00.533 --> 00:05:02.133
and 0 is less than 2, returns 0

00:05:02.133 --> 00:05:03.633
so fibonacci(0) just returns 0

00:05:03.633 --> 00:05:05.133
fibonacci of 0 returns 0

00:05:05.133 --> 00:05:07.800
fibonacci of 1 returns 1

00:05:07.800 --> 00:05:09.821
fibonacci of 0 returns 0

00:05:09.821 --> 00:05:12.733
and then fibonacci of 1 returns 1

00:05:12.733 --> 00:05:14.033
fibonacci of 0 returns 0

00:05:14.395 --> 00:05:20.800
So the whole time the interpreter is processing this recursive function call

00:05:20.800 --> 00:05:23.933
it kinda has to remember all of the previous, how it got there

00:05:23.933 --> 00:05:27.492
because once it eventually gets down to the base cases,

00:05:27.492 --> 00:05:30.733
once it gets down to the n = 1 or 0

00:05:30.733 --> 00:05:31.833
it actually gets a numeric response

00:05:31.833 --> 00:05:32.933
it then has to build up to it's previous reponse

00:05:32.933 --> 00:05:35.148
so fibbonacci(2) right over here

00:05:35.148 --> 00:05:39.148
is 1 + 0

00:05:39.148 --> 00:05:42.687
fibbonacci of(2) is going to simplify to 1

00:05:42.687 --> 00:05:47.000
This fibonacci(3) is fibonacci(2) + fibbonacci(1)

00:05:47.000 --> 00:05:48.129
those get simplified to 1

00:05:48.205 --> 00:05:52.095
so this is going to be 1 + 1

00:05:52.095 --> 00:05:53.363
so this is going to be 2

00:05:53.363 --> 00:05:54.415
We go over here fibbonacci of(2)

00:05:54.415 --> 00:05:58.744
fibbonacci(1) + fibbonacci(0) = 1

00:05:58.744 --> 00:06:00.118
fibbonacci(2)

00:06:00.118 --> 00:06:03.175
1+0 is 1

00:06:03.175 --> 00:06:05.252
We go over to fibonacci of 1, this is 1

00:06:05.252 --> 00:06:06.138
And now we go up to this level

00:06:06.138 --> 00:06:10.000
we're kind of retructuring back until we get back to the original function call

00:06:10.000 --> 00:06:12.133
And I'm not going to go ito the details

00:06:12.133 --> 00:06:13.636
about how the interpreter is actually doing that

00:06:13.636 --> 00:06:15.800
because this is acutally a fascinating discussion

00:06:15.800 --> 00:06:17.933
but I'll just think about how we think about what is happening

00:06:17.933 --> 00:06:20.267
during in thisrecursive function call, and why, why is is working

00:06:20.267 --> 00:06:23.733
why is it gving us the right answer

00:06:23.733 --> 00:06:25.400
And then we go over here fibonacci(4)

00:06:25.400 --> 00:06:29.052
well fibonacci(4), the fourth fibonacci term

00:06:29.144 --> 00:06:31.759
is the sum of the third and the second fibonacci term

00:06:31.759 --> 00:06:33.733
which we have already figured out

00:06:33.733 --> 00:06:37.738
they are two and one, you just take their sum and get 3

00:06:37.738 --> 00:06:41.200
the third fibonacci term, by the definition of the fibonacci sequence

00:06:41.200 --> 00:06:44.067
is the sum of the first and the second term

00:06:44.067 --> 00:06:46.636
those are each one

00:06:46.636 --> 00:06:49.175
the sum of one plus one is two

00:06:49.175 --> 00:06:51.882
the fifth term, the fifth fibonacci number

00:06:51.913 --> 00:06:54.013
the fifth fibonacci term

00:06:54.013 --> 00:06:56.867
is the sum of the fourth and the third terms

00:06:56.867 --> 00:06:58.887
those are three and two

00:06:58.887 --> 00:07:01.338
so three plus two is five

00:07:01.338 --> 00:07:05.067
so this things right over here is going to evaluate to 5

00:07:05.067 --> 00:07:07.133
So hopefully that clarifies a little bit

00:07:07.133 --> 00:07:09.600
about how this recusive program is actually working

00:07:09.600 --> 00:07:11.308
what's neat about it is

00:07:11.308 --> 00:07:13.467
that it wouldn't work if you didn't go down and define

00:07:13.467 --> 00:07:15.800
the base cases of fibonacci(1) and fibonacci(0)

00:07:15.800 --> 00:07:17.733
It would just keep calling itself forever, and never get anywhere

00:07:17.733 --> 00:07:21.400
and what they key with recursion is that it can call itself

00:07:21.400 --> 00:07:25.133
as long as every time it calls itself

00:07:25.133 --> 00:07:27.600
it's making its way down to the base cases

00:07:27.600 --> 00:07:29.400
so at some point

00:07:29.400 --> 00:07:31.267
if it keeps calling itself, it keeps calling itself

00:07:31.267 --> 00:07:35.846
eventually it's able to build back those calls

00:07:35.846 --> 00:07:37.452
to get back to the base cases

00:07:37.452 --> 00:07:40.467
and then recontruct what the original value was from that

00:07:40.467 --> 00:07:42.467
and that's why its working

00:07:42.467 --> 00:07:45.200
each call to fibonacci is a simpler version

00:07:45.200 --> 00:07:46.533
I have a lower n

00:07:46.533 --> 00:07:48.333
and eventually my n are going to get to the base cases

00:07:48.333 --> 00:07:55.733
which will actually give me actual values

00:07:55.733 --> 00:07:57.600
which I can then recontruct for our original calls

00:07:57.600 --> 00:07:59.000
hopefully that helps a little bit

00:07:59.000 --> 00:07:59.500
recursion can be confusing

00:07:59.500 --> 00:08:00.000
but at the same time

00:08:00.082 --> 99:59:59.999
it can also be elegant and beautiful in it's own way


Recursive Fibonacci Example

WEBVTT
Kind: captions
Language: en

00:00:00.500 --> 00:00:02.250
If you haven't tried
already to write

00:00:02.250 --> 00:00:04.740
your own recursive
Fibonacci function,

00:00:04.740 --> 00:00:08.039
I really encourage you to do
so, or at least to try to do so.

00:00:08.039 --> 00:00:10.980
Give it a serious attempt
before you watch this video.

00:00:10.980 --> 00:00:14.350
But if you have done that,
or if you're feeling lazy,

00:00:14.350 --> 00:00:16.160
I guess you can
watch this video.

00:00:16.160 --> 00:00:18.780
And so we're going to try to
write a recursive Fibonacci

00:00:18.780 --> 00:00:22.130
function according to the specs
that I laid out when I first

00:00:22.130 --> 00:00:25.400
set up the challenge to
write a Fibonacci function.

00:00:25.400 --> 00:00:31.790
So I'll call this Fibonacci
again, and it'll take it.

00:00:31.790 --> 00:00:33.870
It has a parameter here, n.

00:00:33.870 --> 00:00:36.546
So someone will have to
pass an argument to it

00:00:36.546 --> 00:00:39.450
to say which term do we want
of the Fibonacci sequence,

00:00:39.450 --> 00:00:41.200
but here we're going
to do it recursively.

00:00:41.200 --> 00:00:43.241
And what we're going to
see is that it's actually

00:00:43.241 --> 00:00:47.440
very intuitive to write a
Fibonacci function recursively.

00:00:47.440 --> 00:00:48.940
It's almost like magic.

00:00:48.940 --> 00:00:50.981
And later we'll see,
though, it's not necessarily

00:00:50.981 --> 00:00:53.890
the most efficient way to
write a Fibonacci function.

00:00:53.890 --> 00:00:55.970
So what I'm going to do
is whenever you write

00:00:55.970 --> 00:00:57.550
any recursive
program, you really

00:00:57.550 --> 00:00:59.560
need to think about the
base cases really well.

00:00:59.560 --> 00:01:02.540
And the base cases of the
Fibonacci sequence, there's

00:01:02.540 --> 00:01:03.630
actually two base cases.

00:01:03.630 --> 00:01:05.830
There's the 0-th term
and the first term,

00:01:05.830 --> 00:01:07.630
and those are really
given by definition.

00:01:07.630 --> 00:01:16.044
So let's just say if we want the
0-th term, if n is equal to 0,

00:01:16.044 --> 00:01:17.710
well, that means you
want the 0-th term,

00:01:17.710 --> 00:01:20.070
and the 0-th term is actually 0.

00:01:20.070 --> 00:01:29.830
And you could say else
if n is equal to 1,

00:01:29.830 --> 00:01:37.800
then you can return 1,
because the 0-th term is 0.

00:01:37.800 --> 00:01:40.870
The first term in the
Fibonacci sequence is 1.

00:01:40.870 --> 00:01:44.470
And now here is where a little
bit of the magic happens.

00:01:44.470 --> 00:01:47.730
Otherwise return-- and
this is really cool

00:01:47.730 --> 00:01:55.980
here-- the Fibonacci
of n minus 1.

00:01:55.980 --> 00:01:58.900
So whatever the previous term
in the Fibonacci sequence

00:01:58.900 --> 00:02:06.790
is plus Fibonacci of n minus 2.

00:02:06.790 --> 00:02:08.366
And I think this should work.

00:02:08.366 --> 00:02:10.740
And that's why it seems magical,
because all we're saying

00:02:10.740 --> 00:02:12.960
is, hey, look, if you want
the 0-th term, it's 0.

00:02:12.960 --> 00:02:14.840
If you want the
first term, it's 1--

00:02:14.840 --> 00:02:17.460
Let me put two spaces here--
if you want the first term,

00:02:17.460 --> 00:02:19.820
it is 1.

00:02:19.820 --> 00:02:23.010
If you want any other term,
so else-- and this is else

00:02:23.010 --> 00:02:25.390
if-- but else if neither
of these are true,

00:02:25.390 --> 00:02:28.240
if it's neither 0 nor 1,
if it's some other number,

00:02:28.240 --> 00:02:29.656
it's going to be--
and we're going

00:02:29.656 --> 00:02:33.160
to assume that you're inputting
some non-negative integer

00:02:33.160 --> 00:02:35.270
over here.

00:02:35.270 --> 00:02:38.200
This would break down if you
did some non-negative integer

00:02:38.200 --> 00:02:40.530
over the non-negative term.

00:02:40.530 --> 00:02:43.160
But assuming you have
a non-negative integer,

00:02:43.160 --> 00:02:46.230
then you say, look, if it's
not the 0-th or the first term,

00:02:46.230 --> 00:02:51.190
then just take the Fibonacci
of whatever one term

00:02:51.190 --> 00:02:54.409
less than it is, plus
two terms less than that.

00:02:54.409 --> 00:02:55.450
And you could try it out.

00:02:55.450 --> 00:02:59.160
For example, let's say
you took Fibonacci of 2.

00:02:59.160 --> 00:03:01.970
Then n is not 0, so you won't
do this, and n is not 1,

00:03:01.970 --> 00:03:02.970
so you wouldn't do this.

00:03:02.970 --> 00:03:05.210
So you'll say return
Fibonacci of 2 minus 1.

00:03:05.210 --> 00:03:09.080
So that's Fibonacci of 1,
plus Fibonacci of 2 minus 2,

00:03:09.080 --> 00:03:10.495
plus Fibonacci of 0.

00:03:10.495 --> 00:03:13.580
Well, we know Fibonacci
of 1 evaluates to 1,

00:03:13.580 --> 00:03:17.520
and Fibonacci of 0 evaluates
to 0, so it'll be 1 plus 0,

00:03:17.520 --> 00:03:19.440
or just 1.

00:03:19.440 --> 00:03:20.580
And you could keep going.

00:03:20.580 --> 00:03:21.850
Try Fibonacci of 3.

00:03:21.850 --> 00:03:25.299
It'll work because we know
Fibonacci of 0, 1, or 2 works.

00:03:25.299 --> 00:03:26.840
Because Fibonacci
of 3 will boil down

00:03:26.840 --> 00:03:29.140
to Fibonacci of 2
plus Fibonacci of 1,

00:03:29.140 --> 00:03:32.430
we know that Fibonacci of 2
is 1, and Fibonacci of 1 is 1.

00:03:32.430 --> 00:03:35.141
So it'll be 1 plus 1 is 2,
so it'll just keep working.

00:03:35.141 --> 00:03:36.140
And we could try it out.

00:03:36.140 --> 00:03:37.390
Let's just save this.

00:03:37.390 --> 00:03:39.100
Actually I didn't save the
file name, let me just call it

00:03:39.100 --> 00:03:39.985
Recursive Fibonacci.

00:03:46.550 --> 00:03:48.380
It's a dot py file.

00:03:48.380 --> 00:03:50.240
And now let me actually
run the program.

00:03:50.240 --> 00:03:53.115
And all this does is define
the function in my environment.

00:03:53.115 --> 00:03:55.570
And then I'd actually
have to call the function.

00:03:55.570 --> 00:03:57.700
So let me just run it.

00:03:57.700 --> 00:04:04.180
And let me ask for
Fibonacci of 4.

00:04:04.180 --> 00:04:05.420
It gave me the right answer.

00:04:09.930 --> 00:04:12.380
Fibonacci of 10, 55.

00:04:12.380 --> 00:04:14.567
Right answer.

00:04:14.567 --> 00:04:15.650
I could try simple things.

00:04:15.650 --> 00:04:18.600
Fibonacci of 0,
the 0-th term is 0.

00:04:18.600 --> 00:04:19.980
So it all works out.

00:04:19.980 --> 00:04:22.399
So that's why recursion
is kind of magical.

00:04:22.399 --> 00:04:24.190
And if you wanted to
simplify this anymore,

00:04:24.190 --> 00:04:27.040
you might recognize, hey, the
0-th term of the Fibonacci

00:04:27.040 --> 00:04:31.330
sequence is 0; the first term
of the Fibonacci sequence is 1;

00:04:31.330 --> 00:04:34.550
so really if you're
looking for the 0-th term

00:04:34.550 --> 00:04:37.430
or the first term, it's really
the same thing as that number.

00:04:37.430 --> 00:04:39.450
So you could actually, maybe,
simplify this a little bit.

00:04:39.450 --> 00:04:40.908
If you assume that
someone is going

00:04:40.908 --> 00:04:44.670
to input a non-negative
integer over here,

00:04:44.670 --> 00:04:50.140
you could say something
like if n is equal to 0,

00:04:50.140 --> 00:05:01.701
or n is equal to 1, return n.

00:05:01.701 --> 00:05:02.700
Let's see if this works.

00:05:02.700 --> 00:05:05.310
So this simplified our
code up a little bit.

00:05:05.310 --> 00:05:06.750
So let's try to save this.

00:05:06.750 --> 00:05:07.880
And now let's run it.

00:05:11.140 --> 00:05:14.050
And now let's try,

00:05:14.050 --> 00:05:15.240
Fibonacci of 10.

00:05:15.240 --> 00:05:19.120
It still worked, because
really the 0-th term is 0,

00:05:19.120 --> 00:05:23.020
and the first term is 1.

00:05:23.020 --> 00:05:26.230
So it works there, or you
could even simplify it more.

00:05:26.230 --> 00:05:30.630
If n is less than 2, that should
also work, because then if it's

00:05:30.630 --> 00:05:32.940
0 or 1 you're going to return
n, and otherwise you're

00:05:32.940 --> 00:05:36.560
going to do all
of this business.

00:05:36.560 --> 00:05:37.710
So let's try that out.

00:05:37.710 --> 00:05:39.190
So we ran it.

00:05:39.190 --> 00:05:46.510
And let's try Fibonacci of 10.

00:05:46.510 --> 00:05:48.180
Now, it seems like
the computer is

00:05:48.180 --> 00:05:50.200
able to do this really
quickly, and all of that,

00:05:50.200 --> 00:05:51.180
and all the rest of it.

00:05:51.180 --> 00:05:54.310
But if you really want to
see all of the work that's

00:05:54.310 --> 00:05:56.600
being done, all the
calls to Fibonacci,

00:05:56.600 --> 00:05:59.130
what you can do is--
let's put a print in here.

00:05:59.130 --> 00:06:01.010
So that every time
Fibonacci gets called,

00:06:01.010 --> 00:06:10.080
it prints the text
Fibonacci colon.

00:06:10.080 --> 00:06:15.250
And then I'm going to add
to that string of text

00:06:15.250 --> 00:06:18.150
what the argument is.

00:06:18.150 --> 00:06:20.840
So the argument in
this is going to be n,

00:06:20.840 --> 00:06:24.830
and I want to make sure that
it is viewed as a string.

00:06:24.830 --> 00:06:26.610
I'm casting it to a string.

00:06:26.610 --> 00:06:28.575
That's essentially
just taking an integer

00:06:28.575 --> 00:06:30.200
and making sure it's
a string, and then

00:06:30.200 --> 00:06:32.860
when I add to this string,
it'll make it one big string.

00:06:32.860 --> 00:06:34.420
But let's see what this does.

00:06:34.420 --> 00:06:38.870
I'm going to try it out
with lower values first.

00:06:38.870 --> 00:06:40.400
So the function is defined.

00:06:40.400 --> 00:06:47.050
So let me write Fibonacci of 3.

00:06:47.050 --> 00:06:50.320
So notice when I called,
it called Fibonacci of 3.

00:06:50.320 --> 00:06:52.820
Then to do Fibonacci of 3,
it had to call Fibonacci of 2

00:06:52.820 --> 00:06:54.590
and Fibonacci of 1.

00:06:54.590 --> 00:06:59.240
And then to do Fibonacci of 2,
it had to call Fibonacci of 0

00:06:59.240 --> 00:07:00.750
and Fibonacci of 1.

00:07:00.750 --> 00:07:03.370
So it had to do all of
these calls to Fibonacci

00:07:03.370 --> 00:07:05.720
just to calculate
the Fibonacci of 3,

00:07:05.720 --> 00:07:07.590
and then eventually it
gave you the answer.

00:07:07.590 --> 00:07:10.200
If you did Fibonacci of 10,
it's going to go really crazy.

00:07:10.200 --> 00:07:11.650
So I'm not even
going to do that.

00:07:11.650 --> 00:07:14.250
Let me just do Fibonacci
of 6, and you're

00:07:14.250 --> 00:07:17.050
going to see how many calls
to Fibonacci it had to do.

00:07:17.050 --> 00:07:21.130
So it had to do all of
these calls to Fibonacci.

00:07:21.130 --> 00:07:24.760
So it was actually a very
computationally intensive thing

00:07:24.760 --> 00:07:25.600
to do.

00:07:25.600 --> 00:07:27.194
But the computers
are super fast,

00:07:27.194 --> 00:07:28.360
so you don't even notice it.

00:07:28.360 --> 00:07:29.710
But in the future we'll
talk a little bit more

00:07:29.710 --> 00:07:31.970
of how do we think about
how computationally

00:07:31.970 --> 00:07:37.210
intensive different ways of
implementing a function can be.


Stepping Through Iterative Fibonacci Function

WEBVTT
Kind: captions
Language: en

00:00:00.660 --> 00:00:02.800
So we can make sure
we really understand

00:00:02.800 --> 00:00:06.131
what's happening in this little
iterative Fibonacci function

00:00:06.131 --> 00:00:07.880
that I wrote, I'm going
to step through it

00:00:07.880 --> 00:00:09.140
with a particular example.

00:00:09.140 --> 00:00:11.056
So we're going to assume
that this function is

00:00:11.056 --> 00:00:12.700
called with the argument 5.

00:00:12.700 --> 00:00:15.520
So we want the fifth term
in the Fibonacci sequence

00:00:15.520 --> 00:00:18.540
starting at where we're
indexing things starting at 0.

00:00:18.540 --> 00:00:20.877
So the 0th term will
be really sometimes

00:00:20.877 --> 00:00:22.710
what you would imagine
the first term to be.

00:00:22.710 --> 00:00:24.150
We'll start at the 0th term.

00:00:24.150 --> 00:00:31.670
So we're assuming that
Fibonacci of 5 is called.

00:00:31.670 --> 00:00:34.060
So we want the fifth term
starting at the 0th term.

00:00:34.060 --> 00:00:35.970
So we just want the fifth one.

00:00:35.970 --> 00:00:38.310
And so when we go
into this program,

00:00:38.310 --> 00:00:41.540
terms is defined as
this list right here

00:00:41.540 --> 00:00:44.270
that just has two
elements, 0 and 1.

00:00:44.270 --> 00:00:46.990
So what I've done here is,
I'm going to really focus

00:00:46.990 --> 00:00:48.340
on what happens in this loop.

00:00:48.340 --> 00:00:50.950
So as we enter into this
loop, the first time

00:00:50.950 --> 00:00:53.210
that we enter into
this loop, terms

00:00:53.210 --> 00:00:58.536
is referring to a list that
has just 0 and 1 in it.

00:00:58.536 --> 00:01:00.410
And we did that because,
by definition, those

00:01:00.410 --> 00:01:04.590
are the first two terms
of the Fibonacci sequence.

00:01:04.590 --> 00:01:06.370
And then also entering
into the loop,

00:01:06.370 --> 00:01:11.510
we define i as being equal to 2.

00:01:11.510 --> 00:01:13.150
And we do that,
because we've already

00:01:13.150 --> 00:01:16.800
defined the 0th term
and the first term.

00:01:16.800 --> 00:01:19.240
And so now we want to,
as we enter the loop,

00:01:19.240 --> 00:01:25.150
we want to add the second
term as we go into the loop.

00:01:25.150 --> 00:01:28.130
And then we say, while i
is less than or equal to n.

00:01:28.130 --> 00:01:31.310
Well, we already
know that n is 5.

00:01:31.310 --> 00:01:32.970
So in the scope
of this function,

00:01:32.970 --> 00:01:36.750
while we are running
it, the variable n is 5.

00:01:36.750 --> 00:01:40.750
And so clearly i is
less than or equal to 5,

00:01:40.750 --> 00:01:43.180
so we will run this
code right over here.

00:01:43.180 --> 00:01:44.802
And we're appending to terms.

00:01:44.802 --> 00:01:46.260
So we're going to
append something.

00:01:46.260 --> 00:01:48.790
So entering into this clause,
this is what i, in terms,

00:01:48.790 --> 00:01:49.610
look like.

00:01:49.610 --> 00:01:52.650
But it looks like we're going
to append something to terms.

00:01:52.650 --> 00:01:57.190
So terms is going to look like--
terms is going to be 0 and 1.

00:01:57.190 --> 00:01:58.870
And then we're going
to add something.

00:01:58.870 --> 00:02:01.520
We're going to add something
right over here to terms.

00:02:01.520 --> 00:02:03.270
And what is that thing
we're going to add?

00:02:03.270 --> 00:02:04.644
Well, we're going
to add whatever

00:02:04.644 --> 00:02:06.329
this business over here is.

00:02:06.329 --> 00:02:08.370
It seems complicated, but
when you break it down,

00:02:08.370 --> 00:02:10.070
it actually doesn't
look too bad.

00:02:10.070 --> 00:02:12.400
In this situation,
what is i minus 1?

00:02:12.400 --> 00:02:15.580
Well, i is 2, so in this
situation, i minus 1

00:02:15.580 --> 00:02:19.000
is going to be 1,
and i minus 2 is

00:02:19.000 --> 00:02:23.140
going to be 0, as we go
through this iteration.

00:02:23.140 --> 00:02:26.230
The first term in
terms, the first term,

00:02:26.230 --> 00:02:27.530
so this is the 0th term.

00:02:27.530 --> 00:02:28.900
This is the first term.

00:02:28.900 --> 00:02:31.800
So the first term in
terms is literally a 1.

00:02:31.800 --> 00:02:34.800
So this whole thing is 1.

00:02:34.800 --> 00:02:39.850
And the 0th term in terms,
remember terms, the 0th term

00:02:39.850 --> 00:02:43.880
in terms-- actually I
should write it this way.

00:02:43.880 --> 00:02:46.090
The list is called terms.

00:02:46.090 --> 00:02:50.350
The 0th term in terms-- this
is what term looks like right

00:02:50.350 --> 00:02:51.950
now-- is going to be 0.

00:02:51.950 --> 00:02:54.310
So this whole thing is
going to be 0, as well.

00:02:54.310 --> 00:02:56.910
So it's 1 plus 0, which is 1.

00:02:56.910 --> 00:02:59.390
And that's what we're
appending to terms.

00:02:59.390 --> 00:03:02.110
So we're going to
append a 1 over here.

00:03:02.110 --> 00:03:04.540
And then we say i is
equal to i plus 1.

00:03:04.540 --> 00:03:07.370
Well i is, right
now, equal to 2.

00:03:07.370 --> 00:03:09.210
So you're going to
add 2 plus 1 is 3.

00:03:09.210 --> 00:03:13.100
And that's going to be
the new value for i.

00:03:13.100 --> 00:03:15.480
And then we go back,
we loop back up

00:03:15.480 --> 00:03:17.710
to the beginning
of the while loop.

00:03:17.710 --> 00:03:19.760
And we say while i is
less than or equal to n.

00:03:19.760 --> 00:03:21.740
Well, now i is a
little bit closer to n.

00:03:21.740 --> 00:03:22.500
It's 3 now.

00:03:22.500 --> 00:03:25.560
But it's still less
than or equal to 5.

00:03:25.560 --> 00:03:27.250
So now we evaluate this again.

00:03:27.250 --> 00:03:29.270
So once again,
entering into the loop,

00:03:29.270 --> 00:03:33.550
terms now looks
like this-- 0, 1, 1.

00:03:33.550 --> 00:03:35.670
I looks like this-- 3.

00:03:35.670 --> 00:03:38.510
It's really the same values
we had exiting the loop.

00:03:38.510 --> 00:03:40.420
And now we evaluate
right over here.

00:03:40.420 --> 00:03:42.080
We're going to add
something to terms.

00:03:42.080 --> 00:03:44.622
So terms right now is 0, 1, 1.

00:03:44.622 --> 00:03:46.080
We're going to add
something to it.

00:03:46.080 --> 00:03:47.290
And what is that something?

00:03:47.290 --> 00:03:52.360
It's going to be the i
minus 1th term of terms.

00:03:52.360 --> 00:03:53.143
So what's the i?

00:03:53.143 --> 00:03:54.820
I over here is 3.

00:03:54.820 --> 00:03:56.940
3 minus 1 is 2.

00:03:56.940 --> 00:03:59.270
So this right over here
is now going to be 2.

00:03:59.270 --> 00:04:03.670
So it's going to be the
second indexed term in terms.

00:04:03.670 --> 00:04:07.240
So this is the 0th,
the first, the second.

00:04:07.240 --> 00:04:12.250
So this over here is going to
evaluate to 1, plus i minus 2.

00:04:12.250 --> 00:04:13.800
Well, i is now 3.

00:04:13.800 --> 00:04:15.720
3 minus 2 is 1.

00:04:15.720 --> 00:04:18.959
So plus the first term in terms.

00:04:18.959 --> 00:04:20.769
This is the 0th term
and the first term.

00:04:20.769 --> 00:04:25.950
So this is also going
to be a 1 over here.

00:04:25.950 --> 00:04:28.020
This is going to
be 1 plus 1, or 2.

00:04:31.300 --> 00:04:33.407
So we're going to
append a 2 to terms.

00:04:33.407 --> 00:04:34.740
That's what this does over here.

00:04:34.740 --> 00:04:39.810
And then we take whatever i was,
which is i, in this iteration,

00:04:39.810 --> 00:04:41.690
i is 3, and we're
going to add 1 to it

00:04:41.690 --> 00:04:43.010
and redefine i to be that.

00:04:43.010 --> 00:04:44.060
So 3 plus 1 is 4.

00:04:44.060 --> 00:04:46.010
And that's the new i.

00:04:46.010 --> 00:04:49.440
Then we go again to the
beginning of the loop.

00:04:49.440 --> 00:04:50.990
While i is less
than or equal to n.

00:04:50.990 --> 00:04:52.380
Well, i is now 4.

00:04:52.380 --> 00:04:54.500
n is is still 5.

00:04:54.500 --> 00:04:55.730
4 is less than or equal to 5.

00:04:55.730 --> 00:04:57.489
So we run this again.

00:04:57.489 --> 00:04:59.030
And once again, we
need to figure out

00:04:59.030 --> 00:05:00.590
what terms of i minus 1 are.

00:05:00.590 --> 00:05:04.370
Let me write it over here, since
this is getting kind of messy.

00:05:04.370 --> 00:05:06.460
Let me actually clear
this up, because I

00:05:06.460 --> 00:05:08.720
want to be able to
read it clearly.

00:05:08.720 --> 00:05:10.230
So let me clear this.

00:05:10.230 --> 00:05:12.010
So now in this loop,
let's just think

00:05:12.010 --> 00:05:15.400
about what-- so now, let
me write it over here.

00:05:15.400 --> 00:05:18.330
Entering the loop
now, i is equal to 4--

00:05:18.330 --> 00:05:21.300
I'm going to do that same orange
color-- entering into the loop

00:05:21.300 --> 00:05:22.420
i is equal to 4.

00:05:22.420 --> 00:05:27.050
And the terms has now been-- it
now has four elements in it--

00:05:27.050 --> 00:05:31.300
0, 1, 1, and 2.

00:05:31.300 --> 00:05:34.420
And now 4 is still less than
or equal to 5, so we do this,

00:05:34.420 --> 00:05:37.120
and we have to figure out
what terms of i minus 1 is.

00:05:37.120 --> 00:05:40.160
Well, now i minus 1 is 3, right?

00:05:40.160 --> 00:05:44.600
4 minus 1 is 3, so
terms of-- not terms of.

00:05:44.600 --> 00:05:49.740
I should say the third element
in terms is 0, 1, 2, 3.

00:05:49.740 --> 00:05:51.690
So it's going to
be-- this whole thing

00:05:51.690 --> 00:05:54.025
is going to be this
2 right over here.

00:05:54.025 --> 00:05:55.400
So we're going to
take that 2-- I

00:05:55.400 --> 00:06:00.220
know you can't read that--
we're going to take this 2,

00:06:00.220 --> 00:06:04.360
and we're going to add it to
terms, the i minus 2th terms.

00:06:04.360 --> 00:06:07.610
I is 4, 4 minus 2 is 2.

00:06:07.610 --> 00:06:11.200
The second term in
terms is 0, 1, 2.

00:06:11.200 --> 00:06:13.800
It's a 1.

00:06:13.800 --> 00:06:15.400
It's this 1 right over here.

00:06:15.400 --> 00:06:19.280
So we're adding this 2
plus this 1 to now get 3.

00:06:19.280 --> 00:06:22.710
And we're taking-- so this whole
thing-- when you add it it's 3.

00:06:22.710 --> 00:06:24.570
And we are appending
it to terms.

00:06:24.570 --> 00:06:29.010
So terms was 0, 1, 1, 2.

00:06:29.010 --> 00:06:32.030
But now we are appending
a 3 to the end of it.

00:06:32.030 --> 00:06:35.056
We're appending a
3 to the end of it.

00:06:35.056 --> 00:06:36.930
And then we're saying
i is equal to i plus 1.

00:06:36.930 --> 00:06:41.110
I was 4, 4 plus 1 is 5, so
that is what i is equal to now.

00:06:41.110 --> 00:06:42.210
It's equal to 5.

00:06:42.210 --> 00:06:43.830
Then we go the
beginning of the loop.

00:06:43.830 --> 00:06:47.780
i is now going to be equal to 5.

00:06:47.780 --> 00:06:51.850
Terms-- let me write in that
same color for consistency--

00:06:51.850 --> 00:06:58.530
terms are now 0, 1, 1, 2, and 3.

00:06:58.530 --> 00:07:02.020
And then we say, well i is less
than or equal to n. n is 5.

00:07:02.020 --> 00:07:04.020
Well, 5 is still less
than or equal to 5.

00:07:04.020 --> 00:07:06.630
It's equal to 5, so
this still is true.

00:07:06.630 --> 00:07:10.480
So we will execute this clause.

00:07:10.480 --> 00:07:13.330
And now we have to figure
out-- let me clear this out

00:07:13.330 --> 00:07:19.800
again-- now we have to figure
out what is terms of i, well,

00:07:19.800 --> 00:07:23.230
what is the i minus
1th term in terms?

00:07:23.230 --> 00:07:24.640
So now i is 5.

00:07:24.640 --> 00:07:25.940
So it's 5 minus 1.

00:07:25.940 --> 00:07:28.360
So it's the fourth
term in terms.

00:07:28.360 --> 00:07:31.080
So 0, 1, 2, 3, 4.

00:07:31.080 --> 00:07:33.750
So it is this term
right over here.

00:07:33.750 --> 00:07:36.090
It is the 3.

00:07:36.090 --> 00:07:39.270
So this over here is 3 now.

00:07:39.270 --> 00:07:42.765
And then we have to
think about-- let

00:07:42.765 --> 00:07:44.890
me do this in another
color-- and then we

00:07:44.890 --> 00:07:47.080
have i minus 2. i is 5 now.

00:07:47.080 --> 00:07:48.540
5 minus 2 is 3.

00:07:48.540 --> 00:07:54.520
The third term in terms 0,
1, 2, 3 is this over here.

00:07:54.520 --> 00:07:56.980
So this over here is 2.

00:07:56.980 --> 00:07:58.680
So you evaluate
this-- you'll notice

00:07:58.680 --> 00:08:01.250
we're just adding the last
two terms that we had so far.

00:08:01.250 --> 00:08:04.170
This is how we build
our Fibonacci sequence.

00:08:04.170 --> 00:08:05.540
And so 3 plus 2 is 5.

00:08:05.540 --> 00:08:08.710
So we're going to append
the 5 to the end of terms.

00:08:08.710 --> 00:08:15.740
So terms is going to be
0, 1, 1, 2, 3, and then

00:08:15.740 --> 00:08:19.550
we are going to
append a 5 to it.

00:08:19.550 --> 00:08:21.410
And then we say i is
equal to i plus 1.

00:08:21.410 --> 00:08:24.380
So i is equal to 5 plus 1, or 6.

00:08:24.380 --> 00:08:25.214
i is equal to 6 now.

00:08:25.214 --> 00:08:26.671
We go to the
beginning of the loop,

00:08:26.671 --> 00:08:28.900
and it says, while i is
less than or equal to n.

00:08:28.900 --> 00:08:33.669
Well, now i is 6,
and n has been 5.

00:08:33.669 --> 00:08:35.750
6 is not less than
or equal to 5.

00:08:35.750 --> 00:08:41.440
So this is false, So we do
not-- we break out of the loop,

00:08:41.440 --> 00:08:44.980
and we go to, I guess,
we stopped running it.

00:08:44.980 --> 00:08:51.700
And then we go to return
the nth term in terms.

00:08:51.700 --> 00:08:53.090
So remember n was 5.

00:08:53.090 --> 00:08:55.580
So what's the fifth
term if we start at 0?

00:08:55.580 --> 00:08:59.020
So this is the 0th term, first
term, second term, third term,

00:08:59.020 --> 00:09:01.845
fourth term, fifth
term, and we are done.

00:09:01.845 --> 00:09:03.720
And hopefully that gives
you an understanding

00:09:03.720 --> 00:09:05.020
of why this worked.

00:09:05.020 --> 00:09:07.590
And also a little bit of the
logic of how we wrote it.

00:09:07.590 --> 00:09:10.310
It's literally building up
the Fibonacci sequence the way

00:09:10.310 --> 00:09:11.420
you would expect to.

00:09:11.420 --> 00:09:17.170
It started with the first
two terms by definition,

00:09:17.170 --> 00:09:18.932
and then each time
we went to the loop,

00:09:18.932 --> 00:09:20.390
and it added another
term and said,

00:09:20.390 --> 00:09:24.970
hey, the new term is just going
to be the sum of the last term,

00:09:24.970 --> 00:09:27.464
right now, and the
second to last term.

00:09:27.464 --> 00:09:29.880
And add them together, and
then that will be the new term,

00:09:29.880 --> 00:09:31.240
and you keep doing that.

00:09:31.240 --> 00:09:34.610
You keep doing that until
you have, essentially,

00:09:34.610 --> 00:09:37.970
until you've added
that nth term.


While Loops in Python

WEBVTT
Kind: captions
Language: en

00:00:00.329 --> 00:00:02.120
What I want to do in
this video is show you

00:00:02.120 --> 00:00:05.567
that for loops aren't the only
types of loops you would use,

00:00:05.567 --> 00:00:07.400
although, they probably
are the most common.

00:00:07.400 --> 00:00:09.990
You could also use
something like a while loop.

00:00:09.990 --> 00:00:12.350
And maybe the while loop
might be more intuitive,

00:00:12.350 --> 00:00:14.020
at least maybe in
the Python context.

00:00:14.020 --> 00:00:14.595
You can pick.

00:00:14.595 --> 00:00:16.219
So I'm going to write
the same program.

00:00:16.219 --> 00:00:18.100
It's going to do the
exact same thing,

00:00:18.100 --> 00:00:21.270
but instead using a for loop,
I'm going to use a while loop.

00:00:21.270 --> 00:00:23.460
So I'm going to still start
with a sum equal to 0.

00:00:23.460 --> 00:00:26.880
But I'm also going to
start with i equal to 0.

00:00:26.880 --> 00:00:30.270
So the while loop isn't
going to automatically define

00:00:30.270 --> 00:00:32.180
what my i values are.

00:00:32.180 --> 00:00:34.200
And it's not going to
automatically change

00:00:34.200 --> 00:00:35.670
my i values for me.

00:00:35.670 --> 00:00:37.370
I'm going to have to
do that for myself.

00:00:37.370 --> 00:00:40.600
But I want to do the
exact same behavior.

00:00:40.600 --> 00:00:42.882
And I'm going to leave
my old code down here,

00:00:42.882 --> 00:00:44.340
so you can compare
it a little bit.

00:00:44.340 --> 00:00:45.756
But then I'll
delete it, because I

00:00:45.756 --> 00:00:47.540
don't want the same
thing to be run twice.

00:00:47.540 --> 00:00:49.630
So I'm going to
set i equal to 0.

00:00:49.630 --> 00:00:51.630
And this, in case you're
curious, the last time,

00:00:51.630 --> 00:00:54.270
in the for loop, i is only a
valid variable inside the loop.

00:00:54.270 --> 00:00:56.100
Now, I'm defining it
outside of the loop.

00:00:56.100 --> 00:00:58.070
So it's actually
a global variable.

00:00:58.070 --> 00:00:59.700
I'm defining at
the highest level.

00:00:59.700 --> 00:01:02.240
It can be used anywhere
inside of the program.

00:01:02.240 --> 00:01:06.460
And so over here, let's
define a while loop.

00:01:06.460 --> 00:01:08.240
So while-- so I'm
going to keep doing

00:01:08.240 --> 00:01:10.440
what's inside of
the loop clause.

00:01:10.440 --> 00:01:13.810
I'm going to do it
while i is less than 10.

00:01:13.810 --> 00:01:18.580
Remember, we want to do it for
i equals 0, 1, 2, 3, 4, 5, 6, 7,

00:01:18.580 --> 00:01:19.730
8, 9.

00:01:19.730 --> 00:01:22.530
Everything up to, but not
including 10, all of the 0

00:01:22.530 --> 00:01:24.260
through 9.

00:01:24.260 --> 00:01:26.979
So while i is less than 10.

00:01:26.979 --> 00:01:28.770
Do-- what I'm going to
put in this clause--

00:01:28.770 --> 00:01:31.620
once i is not less than
10, or essentially,

00:01:31.620 --> 00:01:33.819
once i is equal to 10,
don't do it anymore.

00:01:33.819 --> 00:01:35.485
So it's going to have
the same behaviors

00:01:35.485 --> 00:01:36.549
as this right over here.

00:01:36.549 --> 00:01:38.090
And what I'm going
to do is I'm going

00:01:38.090 --> 00:01:41.160
to say sum is equal
to sum plus i.

00:01:43.890 --> 00:01:45.910
And then I'm going to print sum.

00:01:45.910 --> 00:01:47.770
So this is the exact
same logic that I

00:01:47.770 --> 00:01:49.240
had when I wrote the for loop.

00:01:49.240 --> 00:01:51.240
But I'm going to have to
add one more line here,

00:01:51.240 --> 00:01:52.990
if I don't want this
thing to run forever.

00:01:52.990 --> 00:01:55.150
Because in the for
loop, you'll remember,

00:01:55.150 --> 00:01:57.560
every time we run this
clause here, as we get out

00:01:57.560 --> 00:02:01.390
of the clause, it'll assign i
to the next element in this list

00:02:01.390 --> 00:02:02.130
here.

00:02:02.130 --> 00:02:04.790
Over here, we haven't
defined how i should change.

00:02:04.790 --> 00:02:07.510
So what we have to
do is in every loop,

00:02:07.510 --> 00:02:09.360
we have to increase
the value of i.

00:02:09.360 --> 00:02:14.110
So we can say i is
equal to i plus 1.

00:02:14.110 --> 00:02:17.250
And I want you to really think
about how these are equivalent,

00:02:17.250 --> 00:02:20.440
how both of these are
equivalent things.

00:02:20.440 --> 00:02:24.360
i's started at 0 before I
even entered into this loop.

00:02:24.360 --> 00:02:27.930
And then sum started at
0 for either of these.

00:02:27.930 --> 00:02:29.780
These say sum is
equal to sum plus i.

00:02:29.780 --> 00:02:31.360
Well, that's the same print sum.

00:02:31.360 --> 00:02:34.060
Now, over here, i will then--
once we go through this loop,

00:02:34.060 --> 00:02:36.270
in the for loop-- i
will then be assigned

00:02:36.270 --> 00:02:39.916
to the next element in
this list, which is 1.

00:02:39.916 --> 00:02:41.790
Over here, there's
nothing to say to do that.

00:02:41.790 --> 00:02:43.289
So we're just going
to say, i is now

00:02:43.289 --> 00:02:45.637
going to be the previous i,
which is 0, and add 1 to it.

00:02:45.637 --> 00:02:47.220
And these are
equivalent, because each

00:02:47.220 --> 00:02:50.375
of these elements in this list,
in range of 10 that we define

00:02:50.375 --> 00:02:52.550
it, are just 1 more
than the previous one.

00:02:52.550 --> 00:02:54.040
So instead, we
could just say, i is

00:02:54.040 --> 00:02:56.710
going to be 1 more
than it was before.

00:02:56.710 --> 00:03:00.930
And so this should produce
the exact same result.

00:03:00.930 --> 00:03:03.180
And actually, if I don't
want to get rid of this code,

00:03:03.180 --> 00:03:05.054
if I want it there just
for future reference,

00:03:05.054 --> 00:03:07.580
but I don't want the
interpreter, the Python

00:03:07.580 --> 00:03:10.315
interpreter to run it, I
could comment them out.

00:03:10.315 --> 00:03:12.440
And you comment things out
by putting a little hash

00:03:12.440 --> 00:03:13.550
sign there.

00:03:13.550 --> 00:03:15.800
So a hash sign there,
that literally says, hey,

00:03:15.800 --> 00:03:18.152
interpreter, ignore
all of this stuff.

00:03:18.152 --> 00:03:20.360
And it could be useful if
you have some code in there

00:03:20.360 --> 00:03:22.430
that you want to just not
be interpreted for now

00:03:22.430 --> 00:03:24.150
and maybe you want
to save it for later.

00:03:24.150 --> 00:03:26.020
And it's also
useful, if you want

00:03:26.020 --> 00:03:28.020
to tell someone who's
actually reading the code,

00:03:28.020 --> 00:03:29.561
if you want to tell
a human being who

00:03:29.561 --> 00:03:32.180
is reading the code
what this code does.

00:03:32.180 --> 00:03:35.805
So for example, you could
write a little comment here.

00:03:35.805 --> 00:03:36.570
What did I do?

00:03:36.570 --> 00:03:37.680
What is that thing?

00:03:37.680 --> 00:03:40.425
You could write a little
comment here that says,

00:03:40.425 --> 00:03:51.570
this while loop calculates
the sum of 0 through 9.

00:03:51.570 --> 00:03:52.790
And you'd say including 9.

00:03:55.154 --> 00:03:57.070
So if someone were to
come back later, instead

00:03:57.070 --> 00:03:59.195
of them having to try to
figure out what this says,

00:03:59.195 --> 00:04:01.400
they could read your
comments, and say, OK.

00:04:01.400 --> 00:04:04.590
This part of the code right here
calculates the sum of 0 to 9.

00:04:04.590 --> 00:04:09.855
And I could say, and places it.

00:04:09.855 --> 00:04:11.230
So when you put
this hash tag, it

00:04:11.230 --> 00:04:12.830
essentially comments
out that line.

00:04:12.830 --> 00:04:14.288
So if I want to go
to another line,

00:04:14.288 --> 00:04:15.820
I have to put another
hashtag there.

00:04:15.820 --> 00:04:21.800
And places it in
the variable sum.

00:04:21.800 --> 00:04:24.090
So then people would know,
I know what it's doing.

00:04:24.090 --> 00:04:26.580
And I know that when
all is said and done,

00:04:26.580 --> 00:04:30.357
that the sum will be placed
in this variable called sum.

00:04:30.357 --> 00:04:32.190
But anyway, let's run
this program, and one,

00:04:32.190 --> 00:04:33.606
show that it ignores
these things.

00:04:33.606 --> 00:04:35.957
If you wrote this text
without a hashtag,

00:04:35.957 --> 00:04:37.790
the interpreter would
say, what is all this?

00:04:37.790 --> 00:04:38.749
This isn't Python code.

00:04:38.749 --> 00:04:40.706
But this is useful for
someone else reading it.

00:04:40.706 --> 00:04:42.610
And it will also
ignore this down here.

00:04:42.610 --> 00:04:47.150
So I can delete this.

00:04:47.150 --> 00:04:49.030
Delete that line
right over there.

00:04:49.030 --> 00:04:51.520
And then let's save it.

00:04:51.520 --> 00:04:52.560
And then let's run it.

00:04:52.560 --> 00:04:55.450
It gives you the exact same
results that we got before.

00:04:55.450 --> 00:04:56.197
So let me run it.

00:04:56.197 --> 00:04:58.030
So these are the results
that we got before.

00:04:58.030 --> 00:05:01.864
You might remember, 0, 1, 3,
6, 10, all the way up to 45.

00:05:01.864 --> 00:05:03.280
Now let's run this
thing, assuming

00:05:03.280 --> 00:05:05.970
that I haven't made any bugs.

00:05:05.970 --> 00:05:07.905
And I get the exact same thing.

00:05:07.905 --> 00:05:09.030
I get the exact same thing.

00:05:09.030 --> 00:05:11.810
So this while loop is
doing the exact same thing.

00:05:11.810 --> 00:05:13.864
It's more explicitly
defining i, I guess.

00:05:13.864 --> 00:05:16.030
And it takes you a little
bit more code to write it.

00:05:16.030 --> 00:05:18.541
But it's the exact
same behavior.