﻿1
00:00:00,450 --> 00:00:04,680
‫So bubble sort is going to be our first sorting algorithm.

2
00:00:04,770 --> 00:00:08,730
‫It's probably the easiest one that we're going to do.

3
00:00:09,420 --> 00:00:15,630
‫So I'm going to bring up an array like this, except I'm going to show it like this.

4
00:00:15,630 --> 00:00:19,290
‫And I had previously shown arrays as being green.

5
00:00:20,100 --> 00:00:25,890
‫And what I'm going to do with the sorting algorithms is I'm not going to show them as green until they

6
00:00:25,890 --> 00:00:26,540
‫are sorted.

7
00:00:26,550 --> 00:00:30,080
‫So anything that's unsorted, we will make blue.

8
00:00:30,090 --> 00:00:36,170
‫So I'm going to go ahead and put this back to explain bubble sort so it works like this.

9
00:00:36,180 --> 00:00:37,710
‫We're going to start with the first item.

10
00:00:37,710 --> 00:00:43,410
‫I'll highlight that an orange and we're going to compare the first item with the second item.

11
00:00:44,240 --> 00:00:47,270
‫And if the first item is bigger than the second item.

12
00:00:48,050 --> 00:00:49,820
‫We're going to switch them now.

13
00:00:49,820 --> 00:00:54,950
‫We're going to compare the second item with the third item, and if it's bigger, we switch them.

14
00:00:54,950 --> 00:00:56,480
‫But in this case, it's not.

15
00:00:56,810 --> 00:01:01,220
‫So now we move to the third item, we compare it to the fourth item, and it's bigger.

16
00:01:01,730 --> 00:01:04,610
‫And we compare it to the next one and the next one.

17
00:01:05,400 --> 00:01:08,870
‫And now the six is sorted.

18
00:01:08,880 --> 00:01:11,790
‫That is the correct spot for the six.

19
00:01:12,150 --> 00:01:17,400
‫Whatever the largest item is, we're going to bubble that all the way up to the top.

20
00:01:18,000 --> 00:01:23,880
‫So that took five steps to get that all the way to the end in a six item array.

21
00:01:24,300 --> 00:01:31,950
‫Then we start over and we compare 2 to 4, 4 to 5, 5 to 1, 5 to 3.

22
00:01:32,310 --> 00:01:34,140
‫And now the five is in the correct spot.

23
00:01:34,140 --> 00:01:37,080
‫So now the second biggest item.

24
00:01:37,750 --> 00:01:39,970
‫Has been bubbled up to the top.

25
00:01:40,470 --> 00:01:42,270
‫But that only took four steps.

26
00:01:42,270 --> 00:01:48,030
‫So each time we go through this, it will take fewer steps, but it is still pretty inefficient.

27
00:01:48,240 --> 00:01:49,560
‫So we'll do this again.

28
00:01:49,560 --> 00:01:51,510
‫2 to 4, 4 to 1.

29
00:01:52,410 --> 00:01:54,100
‫Compare four with three.

30
00:01:54,120 --> 00:01:55,830
‫The fours in the correct spot.

31
00:01:56,250 --> 00:01:58,080
‫The next round will only take two steps.

32
00:01:58,080 --> 00:02:02,100
‫So we'll compare two with one, and then we'll compare two at three.

33
00:02:02,100 --> 00:02:04,800
‫And now three is in the right spot.

34
00:02:04,800 --> 00:02:09,570
‫And on the last one we just compare these two and if they need to be switched, we switch them.

35
00:02:09,870 --> 00:02:14,130
‫If not, these two are now in the correct spot.

36
00:02:14,490 --> 00:02:17,730
‫So I'm going to go ahead and mix these back up like this.

37
00:02:17,730 --> 00:02:23,820
‫We will pick up here in the next video where we code bubble sort.

