 Hello everyone, I am Mrs. Meenakshi Shrigandhi, Assistant Professor from Vulture Institute of Technology, Sholapur. Welcome to the video lecture on bubble sort. Learning outcome. At the end of this session, students will be able to implement bubble sort using C programming. Let us see what is sorting is. Sorting take an unordered collection of elements and make it ordered one. For example, this is an unsorted array where the elements are unordered. So after sorting the elements becomes ordered and this is nothing but a sorted array. Let us see what is the bubble sort is. Bubble sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. The algorithm which is a comparison sort is named for the way smaller or larger elements bubble to the top of the list. Let us see illustration of bubble sort. For this, consider an array A where it consists of six elements. The array elements are placed in this order where the first element is placed at the 0th index and the last element is placed at n minus 1th index. Now in the first iteration of the bubble sort, the comparison of the first two elements is done. Here in the comparison we can see that A of 0th element is greater than A of 1th element. When that is the thing then we need to swap the elements. So we have swapped it and the comparison is done for the next two elements. Here also we can see that A of 1 is greater than A of 2. Then here we need to do the swapping. So here we have done the swapping and the comparison is done for the next two elements. Here also we can see that A of 2 is greater than A of 3rd element. So again here we need to do the swapping between the elements. So after swapping you can see that the elements are placed in this order. Now the comparison is done between these two elements. Here we can see that A of 3 is lesser than A of 4. Now when this happens, when A of 3rd element is lesser than A of 4, no swapping should be done and the elements remain as it is. Now next we are doing the comparison between these two elements. Here we can see that A of 4 is greater than A of 5. So here we need to do the swapping. So after swapping you can see that A of 7 is placed here and this is the end of the first iteration and we can see that the largest element is sorted. In the second iteration again the comparison begins from the very beginning. The first two elements are compared. And here we can see that A of 0 is lesser than A of 1. If it is less then we should not swap these elements. Then they remain as it is. The next comparison is done between the next two elements. And here also we can see that A of 1 is less than A of 2. As it is lesser than, as A of 1 is lesser than A of 2 then here no swapping should be done. So the elements remain as it is. The next comparison is done between the next two elements that is between 47 and 56. And here also we can see that A of 2 is less than A of 3. As it is lesser we have to not do any swapping between these two elements. So they remain as it is. The next comparison is done between the A of 3 and A of 4th element. And here we can see that A of 3 is greater than A of 4. Now here we need to do the swapping. So after swapping you can see that 56 is swapped to this place and this is the end of the second iteration. And at the end of the second iteration we can see that the second largest element is sorted. In the third iteration again the comparison begins from the beginning. The first two elements are compared. And in the comparison we can see that A of 0 is lesser than A of 1. If it is lesser then no swapping is done and the elements remain as it is. And after that we are doing the next comparison between the next two elements. Here also we can see that A of 1 is lesser than A of 2. So no swapping is done. So the element remain as it is. The next comparison is done between the next two elements that is between A of 2 and A of 3. Here we can see that A of 2 is greater than A of 3. So here the swapping should be done between these two elements. And after swapping we can see that 47 is placed at A of 3. So this is the end of the third iteration. And after the end of the third iteration we can see that the third largest element is also sorted. In the fourth iteration the comparison again begins from the very beginning. The comparison is done between the first two elements. Here we can see that A of 0 is lesser than A of 1. So no swapping should be done between this. And the comparison is done between the next two elements. And here we can see that A of 1 is greater than A of 2. So here the swapping is needed. So we need to swap the two elements. So after swapping 34 is placed at A of 2. So after the end of the fourth iteration we can see that the fourth largest element is also sorted. Fifth iteration, in the fifth iteration again the comparison begins from the very beginning. The first two elements are compared. And here we can see that A of 0 is lesser than A of 1. So here no swapping should be done and here the elements remain as it is. So at the end of the fifth iteration we can see that all the elements are sorted. So here in this we can see that in each iteration one of the element is sorted at its proper place. In the next iteration we should ignore this element and we need to compare till the unsorted array only. And in the next iteration we can see that the next two largest elements are sorted and in that iteration we need to do the comparison only till unsorted array only. Let us see the time complexity of bubble sort. The best case time complexity of bubble sort is O of n. Here the best case of the bubble sort is achieved when the array is already sorted and there we need n comparisons to be done. The worst case and the average case time complexity of bubble sort is O n square. So the worst case of bubble sort is achieved when we want to sort the array in ascending order but the given array is in descending order. Try to think an answer. What is the algorithm for bubble sort? Pause the video for some time and note down the answer in your book. Algorithm for bubble sort. Input an array A of n elements. Initialize i equal to 0 and temp equal to 0. Here we need to repeat the steps 4 to 9 while i is lesser than n minus 1. In the loop, in the while loop every time we need to set j equal to 0. And we need to repeat steps 6 to 8 while j is lesser than n minus i minus 1. And in the next step we need to check if A of j is greater than A of j plus 1. If it is so then go to step 7 else we need to go to step 8. Here we are swapping the elements stored at A of j and A of j plus 1 by following the below step. We are taking a temporary variable and we are setting that at A of j plus 1. And A of j plus 1 is set to A of j and A of j is set to temp. After that we need to increment j by 1 and in the next step we need to increment i by 1 and after coming out of the two loops we need to print the array elements which are sorted elements. Let us see implementation of bubble sort using C programming. In the first step we are declaring the array of size 50 and other variables integer variables like n, i, j and temp is initialized to 0. Using a print of statement we are asking the user for how many elements he wants to enter in the array. And by using scanif we are collecting that number into n. Here we are using the for loop which is initialized to 0 and the loop is repeated till i reaches to n and in each iteration we are taking one element from the user and that element is placed at the ith position of the array. In the next step we are initializing i to 0 and we are using a while. The while loop is repeated till i is lesser than n minus 1. When the condition becomes true every time we are setting j equal to 0 and here we are using another while loop till j is lesser than n minus i minus 1. This while loop continues and in this we are checking whether the jth element of the array is greater than j plus 1th element of the array. If this condition becomes true then we need to do the swapping. So the swapping is taken by the swapping is done by using a temporary variable. In this variable we are placing the jth element of the array and at the jth position of the array we are placing the j plus 1th element of the array and in the j plus 1th position of the array we are placing the value of the time. So here we are doing the swapping between a of jth element and a of j plus 1th element. And after this is done we need to increment j by 1. This ends the inner while loop. After coming out of the inner while loop we need to increment i by 1. This terminates the outer while loop. The outer while loop repeats till i is lesser than n minus 1. Once it comes out of the loop all the elements of the array are sorted and in the next step we need to print the array elements after sorting. So here again we are using a loop and in each iteration we are printing the ith element of the array. So here it gives the sorted elements of the array. So this was the implementation of bubble sort using C programming. These are my references.