 Hello and welcome to this video lecture. I am Vinit Thribhuan from the Computer Science and Engineering Department here at Vailchand Institute of Technology, Solapur and today we are going to see the first video from this video lecture series on sorting. So today I am going to introduce you to the topic of sorting and then we shall see the different types of sorting. The first one that we are going to see today is bubble sort. So as you go through this video lecture at the end of this session you will understand the bubble sorting algorithm as we are going to discuss about the algorithm and also you will be able to apply this sorting technique that we are going to see today to sort a given set of integers. So before we begin with and learn the sorting techniques I want you to refer to the following topics before we proceed. You are encouraged that you go through topics of arrays and loops and if you are well versed to implement arrays and using arrays and loops in C you will be able to implement and apply these sorting techniques to sort the given data. So what exactly does sorting mean? Now you might have come across data which is in random order. It may be the marks that a class of students have obtained in a particular test. It may be a score card or a score sheet of all these students that have qualified a particular exam say for example NEAT exam or CET exam or simply you might be shopping and you want to sort the data or the items according to the increasing order of their prices. For all these things we require sorting. So sorting is simply to arrange the data in ascending or descending order. So whenever we arrange data in chronological order or reverse chronological order we tend to say that we are sorting the data. So sorting the data in ascending order is in the chronological order and in the descending order is in the reverse chronological order. So we have many applications of sorting the primary applications being generating university results where the university wants to display the top three or the top 100 students that have failed well and are the toppers of the institute or the university and there the results of the scores are displayed in a reverse chronological order the highest score being the topper in the list and so on. You might have also tried to sort different types of files in a computer according to the date of their modification or according to their types or as we mentioned earlier you might have tried to sort the sizes of a particular product maybe you want to purchase a shoe and then you want to sort the shoes available according to their sizes just to make sure that the shoe size that you want is available or not. Hence you sort the available shoes according to their sizes. All these are examples of sorting that we have seen in day-to-day life. Now when we speak about bubble sort it is the most simple technique of sorting as long as implementation is concerned. It performs sorting by swapping the adjacent elements if they are not in the sorted order. For instance if we have two elements say in the order 20 and 10 where 20 is the first among the two the items would be swapped and the result would be 10 comma 20 after sorting since they are adjacent elements and they are swapped. So let us try to understand the algorithm of bubble sort. Firstly we have a set of integers say for example we have an array of n elements. So in these n elements we compare a pair of adjacent elements a comma b where a might be the element on the 0th index and b might be the element on the first index or i plus 1 that is 0 plus 1 index. The second step is that we swap the pair if the items are out of order in the case when the first element or the element at the 0th index is greater than the element at the first index. In this case we swap the two elements. Now we continue doing the compare and the swapping process till we reach to the end of the array. If you carefully observe while we scan and come across to the last element the largest element will be at the last position since we are comparing and swapping the adjacent elements. Once the largest item is at the last position it means that it has taken its final place and then we reduce the size of the array by 1 and simply repeat steps number 1 to 4 till we have n remaining as 1 that means the size of the problem has reduced to a single element. This is the pseudo code of the bubble salt algorithm. We have array a of size n and we have an index k running from 0 to n minus 1 where n is the size of the array. Then we have an index i running from 0 to n minus k minus 1 where this will reduce by 1 each time a element finds its final place in the array and then we do the comparison between the two adjacent elements that is i and i plus 1. If the element at ith location is greater than the element at the i plus 1 location then we do the swapping of the elements at both the positions. This happens till we reach at a size of n equals 1 sub array that means the last element to be sorted will find its final position. I want to show you a simulation of bubble salt. So this is hacker earth dot com platform and I am using the visualizer that they have on the platform. Say for example we have five elements in the given order. So let us see how bubble salt works. We move from left to right swapping the adjacent elements. We first compare the elements which are adjacent in position. If the element to the left is greater than the element to the right then it is swapped. Now the next two elements are compared and they are swapped if they are not in order. Next again the adjacent elements are compared and they are swapped if they are not in order. This happens till the greatest element finds the last position and here the element 5 has found its final position. We reduce the size of the sub problem by 1 and now we look at the elements from index 0 to 3. 3 and 4 are compared and since they are in order they are not swapped. 4 and 2 are compared and they will be swapped. Next 4 and 1 are compared and since they are not in order they will be swapped. And here we see that 4 has been finally put in its final position. Next we compare 3 and 2 and swap the elements. We also compare 3 and 1 and then swap the elements. Here 3 has found its final position in the array. Now the sub array is only of two elements. Both these elements are compared and they are swapped. Now 2 is at its final position and the size of the problem has now reduced to 1. So we come to know that the sorting is over. Now at this point in time I want you to pause the video and ponder on the question that how many comparisons are required to sort an array of n size by bubble sort. Pause the video and try to answer the question. So if you carefully see the algorithm and the pseudo code you will come to know that we will require n squared comparisons to sort an element of to sort an array of size n. So this is the reference that I have referred to for the video and for the content. Thank you.