23:04:26 Hello everybody, my name is Melinda and today we're going to talk about Nessed for loops and bubble sort. 23:04:32 Let's start with 4 loops. Nested for looks are basically 2 regular for loops put together. 23:04:39 No nested. They said regular extension of the first one. In this case, it's very important to realize that the inner for loop in R is the J loop would have to run fully before it would go back into the outer for loop and increment I. 23:04:56 And the same process would repeat until the out of 4 loop is finished. So let's have a closer look at our cover. 23:05:05 We're going to start. I at 0. And we're going to print out for the outer for loop the value of i and then the letter a and for the enter for loop we're gonna print out the letter J and the index of J and letter C. 23:05:22 So if we start, we're gonna start with. Hi, I equals 0. So we're going Are you going to print out 0 A because we're in outer for loop? 23:05:33 In this case we're going to enter the NFL loop and start our J at 0. So then we have 0 C. 23:05:41 One C and 2 C. And dill here. In this case, we break out of the inner for loop back out into the outer for loop and increment I. 23:05:51 Our I used to be 0 now it is going to be one so we're going to print out one A What enter again into the inner for loop and we start again from 0, 0, C, one, C, 2, C. 23:06:05 Will break out from the inner for loop back onto the into the outer for loop, increment i to 2. 23:06:12 So we're gonna have 2 A. Then we go back in. To the inner for loop. 0 C one C 2 C and so on until we get to I equal 4 and this is our last iteration of the NRC loop. 23:06:29 0 C one C 2 C Let's see if this is the output we got from the computer. And as expected, we got the same extent output as the computer nested for looks are very useful. 23:06:48 They can help us, transfers to the arrays, but it can also help us with sorting. 23:06:54 So, that leads us to bubble sort. Marvel sort is one of the easiest sorting algorithms. 23:07:00 And let's suppose that we have the following array. We have an array. Of length 4 and starting at indexing a 0. 23:07:11 So 0 1 2 3. And we want to fill it with some random variables. So we have 32. 23:07:18 Any 47 and 11. These 4 numbers are obviously not in order. So this is what we're trying to do what bubble sort is going to help us do. 23:07:26 We will have a closer look at the code after we conceptually understand how bubble sort works. So bubble sort is going to understand how bubble sort works. 23:07:36 So bubble sort is going to compare the first 2 numbers. So bubble sort is going to compare the first 2 numbers and if one it's smaller than the other, it's going to compare the first 2 numbers. 23:07:41 And if one, it's smaller than the other, it's going to swap And if they're not, it's going to leave it the way it is. 23:07:45 In this case, the first number, 32, is not less than 20, but 20 is less than 32. 23:07:50 So they're going to get swapped. So we swap them and then we move forward. 23:07:55 Now we're going to compare 32 and 47. Luckily in this case, 32 is less than 47. 23:08:01 So everything remains the same and we would move forward. We compare 47 and 11. In this case, 11 is less than 47, so we will swap the 2 numbers. 23:08:13 Thus concluding the first pass of Tubble sort. Then we move forward to the second basket double sort. 23:08:20 We will start again from the beginning. And we would check is 20 less than 32. In this case, yes. 23:08:29 So everything remains the same and we move to the next part. We would compare 32 and 11. In this case we would have to swap them because 11 is less than 32. 23:08:36 So we go ahead and swap them. Now we move to the next part. It's 32 less than 47. 23:08:43 Yes. So everything remains the same and we will move to the next path. That would that would be the third task of the bubble sort algorithm. 23:08:52 And the third pass we check again is 20 less than 11. No, 11 is less than 20. In which case we go ahead and swap the 2 numbers. 23:09:02 And we go ahead and check to see if everything else is in order. 20 is less than 32. 23:09:11 32 is less than 47. That's concluding. I don't know. Now let's have a look at the code. 23:09:19 This method. Shows the bubble sort algorithm. So we're going to start with having an integer and which 23:09:33 Will hold the length of our array, our array is called numbers in this case, and we also will have a bullion call swap. 23:09:42 So we're going to start with our outer for loop. I we're going to start it at 0. 23:09:47 Go until the length minus one Less than ninth minus one and we're going to increment every single pass. 23:09:57 We're going to keep all swap bullying at falls for now and only when we swap 2 things we're going to switch it back to true. 23:10:02 Then for the inner forl of the J loop. We're gonna start at 0, which is the first. 23:10:09 Index of the array. And then continue until our length minus i and minus one because we don't want to go we don't want to get out about. 23:10:20 And again, increment. In this case, we're going to use, we're going to compare the numbers at I at J. 23:10:25 To make sure that is bigger than the number at J plus one. And then we're going to use a temporary variable. 23:10:35 Temp, we're going to set it to the number at J and then that number of J, we're going to set it to the numbers in J plus one. 23:10:43 And then numbers are J plus one, we're gonna make it equal to the 10 variable. In this case, we found 2 numbers have to be switched. 23:10:53 So with the 2 numbers and we're going to Turn the swap variable to true. And move on. 23:11:00 Forward if the 2 if everything in the inner loop is in order then Do you already sorted, so, we're going to break out of the method. 23:11:15 And have a look at our main method. 23:11:20 Our main method is going to have our numbers array. And the same numbers we started with in the animation, 32, 2047 and 11. 23:11:31 We're going to go ahead and print the numbers. Before any sorting has been done. So we get the same numbers you can see out here. 23:11:38 32, 2047 and 11. Then we're going to call the bubble sort method that we saw a second ago and pass in the numbers. 23:11:48 Integer right. This method is gonna use the code over here. And then we are going to print out again what has been returned. 23:11:58 So in this case, after the sorting, we can see that we get 1120, 32 and 47, which we can easily see that all the numbers are sorted. 23:12:11 Thank you so much for watching. I hope this helped understand a little bit more about Nested For Loops and double sub algorithms.