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