Category
📚
LearningTranscript
00:00Hello everyone. In my last video, I have explained how the bubble sort takes place. What is the
00:08methodology? What is the architecture to arrange the numbers in numerical order, in sorted
00:15order? So, I have explained that there will be what if you are going to store the values
00:23inside one of the array and you have to store the five elements. So, it's going to start
00:30from 0, 1, 2, 3 and 4. And then if you have to sort, then you have to sort it in numerical
00:41order. So, for that, as you are going to sort the five elements, for that you need the four
00:48iterations. This is the first one, this is the second one, this is the third and the fourth.
00:53Then you are going to get the resultant values in sorted order. As you can see, earlier the
01:01values were in random order. Okay, 14, 13, 15, 12 and 11. And after the iteration took place,
01:13after the methodology of bubble sorting, we find that the values are arranged in numerical order,
01:2311, 12, 13, 14, 15. So, first we need the four iterations, okay, to get the final results. So,
01:32we will apply the outer loop for the total number of iteration, that is 1, 2, 3 and 4,
01:38okay. And inside the outer loop, we will apply one more loop, okay. And in the inner loop,
01:47we will apply such a condition that it will start from 0 and it will go till what, 4th. Then it
01:55will start from 0, but it will go one less than the 4th position, it will go up to what, 3. Then
02:02again it's going to start from 0th position to what, 2 and then from start to what, 1. So,
02:09decrementation will take place from the downside, okay, that we have to see that type of loop we
02:17have to apply inside the outer loop. So, that we have to see, okay. So, I'm going to make the
02:24program of bubble sort. I will save it, prgBubble, okay. Sort, I have given the name to it. First of
02:47all, I will store the values inside the array, okay, having size of 5. So, I take one of the
02:58array variable having size of 5. I need the variables i, j and k. Why I am taking these
03:06three variables, i, j and k, that I will explain, okay. Now, I will give a message here. What I am
03:14going to do is to input values in array, okay. So, that I am going to do, I'm going to store the
03:26values inside the array. For that, I will apply the loop for i equals to 0, i less than equals to 4
03:39and i plus plus. The number of times we have understand how the loop will carry on and how
03:47the values are going to store inside the array, okay. So, that I am not going to explain. So,
03:55we have understand these things in our previous videos. So, in this way, the value is going to
04:00store inside the array A, okay. The loop will carry on and the values are going to store inside
04:06each index position of the array. Now, what I have to do, I have to do the sorting for that.
04:11Again, I will apply the loop for i equals to 0, i less than
04:224, okay and i plus plus as I have to carry on for 4 times.
04:28The iteration will be for what, how many times? 4 times. So, it will start from 0, 0, 1, 2, 3,
04:34okay. So, that is what 4. Now, inside it, I will write down what j equals to 0, j less than 4
04:45minus i. Why I am doing so? I will explain it, okay and j plus plus. Inside it,
04:53I will apply the logical part that is if a, j, okay. If a, j position value is greater than
05:05a, j plus 1 position value. In that case, the swapping should take place that is k equals to
05:14a, j, okay and the value which is there inside the a, j plus 1 position that is going to store
05:25inside a, j position, okay and the value which is there inside k that is going to store inside
05:36a, j plus 1 position, okay. In this way, the swapping will take place.
05:42Once everything is arranged in numerical order, sorted order, I have to display it. So,
05:52I will again apply the rule i equals to 0, i less than equals to 4, i plus plus, okay
06:00and inside it, I will use what printf slash n or you can directly display the values that is a,
06:11i, okay but you need some space also. So, printf, so I will give some space,
06:18okay just to show you that I am displaying some space also after the display of the value, okay.
06:26Let us use what return 0 for the exit status, okay. Everything is fine and I save the program
06:39by using ctrl s, will click on build, it's showing me warning printf something here,
06:56a pointer from integer without a cast,
07:10okay. I haven't used what parameter that is acquired, okay. So, what I should do here,
07:16write down percent, comma and what here that was missing.
07:31Let us again click on build. Now, there is zero error and zero warning.
07:35I hope everything is fine and it should work. Let us see the canf also. I am going to click on
07:45run, okay. Now, I am putting the values 44, 33, 66, 11 and 77. I am going to press enter.
07:57If you see it arranged in sorted order, 11, 33, then 44, then 66, then 77.
08:06So, it's working perfectly. Now, let us understand
08:19how the numbers are arranged in numerical order. So, here I have applied the loop,
08:28the for loop to store the values inside the a array, okay. You all know how the loop will
08:34carry on, how the values will store. The values are in the random order, okay.
08:41In this way, okay, values are stored. To understand, this part is important.
08:49I start from i equals to zero, zero less than four, the condition is true, it will come to the
08:54inner loop, the value of j is what, zero. Now, what is four minus i, that is four minus zero,
09:00that will be what, four. So, zero less than four, the condition is true. The value of j is what,
09:05zero and what, zero plus one, one. So, we are comparing the jth position value with the first
09:12position value. Let us see. The jth position value is 14 and the first position value is what, 13,
09:19okay. Is 14 greater than 13? Yes, 14 is greater than 13. So, interchange will take place.
09:25You see, 13 has gone up and 14 come down, okay. How interchange take place? The value which is
09:31there inside the jth position, that is stored inside the k. The value which is there inside
09:37the first position, that is going to store inside the jth position and the value which is there
09:42inside k, that is going to store inside the first position, interchange take place.
09:48Now, the value of j is going to increment from zero to one. Now, it will come here,
09:54one less than four, the condition is true. Now, we are going to compare the first location
10:00with the second as the value of j is one and here it will be one plus one, two.
10:05So, let us see what is there inside the first, it is 14 and what is inside second, it is 15.
10:12Is 14 greater than 15? No, the condition is false. So, the value is remain as it is,
10:17okay. I hope you understand how
10:24things are working. It is not going to perform this operation as this condition is what false.
10:31Now, the value of j is going to increment from one to two and here it will be two less than
10:38four, the condition is true. Now, this time what the second position is going to compare with the
10:43third. What is there inside the second, it is 15, it is compared with the third, the condition is
10:50true. So, interchange will take place, this operation will take place, the swapping operation,
10:56okay. So, after taking place, the 12 is going to the second position and 15 is going to come
11:03to the third position and then the third is going to compare with the fourth, okay.
11:10What you will see, the 15 will come at the last position, the condition is true,
11:14so interchange take place and come to the last position. Now, when the condition get false,
11:20when the value of j will what, what four, okay, four less than four, the condition will get false,
11:26it will go to the outer loop, the value of i is going to increment to one, one less than four,
11:31the condition is true. Now, four minus one that is what, three. So, how many times the inner loop
11:38is going, inner iteration is going to work, that is one, two and what, three, okay.
11:46And in same way, it going to work for two times, okay, the third iteration and then one time,
11:54then we will get the final result. So, I hope you understand the logical part,
11:59how the things are working. Once the data is now there inside the array in sorted order,
12:06I again applied the loop, display the values and we get the values in sorted order.
12:14I hope you understand. In the next video, I will explain something else. Bye.