 Hello and welcome to this video lecture on selection sort. This is a series of video lectures on sorting methods and algorithms in C language. So this video is the third video in this series. See we have seen how to sort an elements firstly using bubble sort technique and next using insertion sort. In this video we will see how to use selection sort or what is selection sort algorithm. So by the end of this video lecture you will understand the working of selection sort algorithm and also you will be able to apply this technique to sort a given set of integers. So before we go ahead with this video lecture I strongly recommend you to refer to the following topics which will help you to easily understand the concept of sorting using selection sort. You are to be well versed with using arrays with using loops and also you should be able to understand and swap two given elements in an array. If you are well versed with these concepts in C then you are good to go to learn selection sort. So talking about selection sort what we do in selection sort is that we select the element with the minimum value. So when we scan the entire array and find the most smallest valued element what we do is we swap this element with the element in the first position in the array. Say for example I have an array or a randomly ordered array has been given to me as a job to sort it. So I will to sort in the chronological or ascending order. So what I want to do is I want to find the smallest valued element and I will be scanning the entire array just to find the smallest element and once I find the smallest element the algorithm shifts or swaps the smallest element with the element in the first position in the array. That is how the selection sort algorithm works and the same process is repeated for all the elements until the element or the array of elements is sorted. Now you can choose the maximum value in the array and put it in the last position or you can choose the minimum value and put it in the first position. Both ways selection sort will give you the same result when you are sorting the array in the chronological order. In our case in this video lecture I will be looking for the minimum valued element and swap it with the element in the first position in the array. Let us go ahead and see the algorithm. Let us look at the algorithm of selection sort. Firstly we start scanning the array one element at a time. We set the first element as the minimum element and then start scanning the other elements in the array. Now for each of the unsorted elements in the array when we scan the next element if the next element is less than the current minimum value we set it as the minimum value. This scanning and setting of the minimum value is done till we cover all the elements in the array. Once we find out the most least valued that is the least valued element in the array what we do is we swap that minimum valued element with the first unsorted element in the array or the first element or the element at the first position in the array and we repeat this process for the remaining n-1 elements till the algorithm sorts all the elements in the array. Let us look at the pseudocode of the algorithm. This is the pseudocode of selection sort. Now this algorithm takes an integer array of size n and minimum index is assigned to the element at position i where i is currently at 0. Then j is at a position of i plus 1 and it goes scanning the array till j is less than n and if j that is the element at position j is less than the element at minimum index what we do is we set j index as the minimum index and then after all the elements till size n-1 are checked we find the element with the least value we change or swap the minimum element with the first element where i is pointing to the first element in the array. Now let us look at the simulation of selection sort. So let us look at the simulation of selection sort. So this is visualalgo.net where the simulation of various sorting techniques is given for us so that we can understand it clearly. So here is an array of size 5 in random order and we are going to see the simulation step after step. So initially 5 is selected as the minimum valued element and then the entire array is scanned just to see if any value is less than 5 if it is less than 5 then that value becomes the new minimum value. Here 4 becomes the new minimum value. Now 2 becomes the minimum value since it is less than 4. Now 3 is scanned but since 2 is less than 3 2 remains the minimum and after 1 is scanned so 1 becomes the new minimum and now 1 will be swapped with the element in the first position of the array that is 5. So the minimum valued element that is 1 has now been sorted and it has found its final position in the array. Now we are going to check for the rest of the array that is 4, 2, 3 and 5. So 4 becomes the new minimum it is compared with 2 and since 2 is less than 4 2 becomes the new minimum. 2 is compared with 3 so 2 remains the minimum as it is and 2 is compared with 5 so it remains the minimum. So 2 is now compared and swapped with the first element in the unsorted array so 2 comes to its final position in the array. Now 4 becomes the new minimum in the unsorted array 4 and 3 are compared and 3 becomes the new minimum 3 is compared with 5 and 3 stays as the minimum value and now 3 is swapped with the element in the third position and gets to its final position. Next 4 becomes the minimum value in the unsorted array this is compared with 5 since it is less than 5 it remains as the minimum value and then here we have a sorted array using selection sort. This is how selection sort works. Okay, time for a quick question. I want you to pause the video and answer the question. The question is what changes have to be made in the current algorithm that we have discussed to sort the data in the reverse order in reverse chronological order. Pause the video, make the changes and answer the question. Okay, so if you have understood the question and you have gone through the algorithm once again so the answer is if you want to sort it in the reverse chronological order instead of looking for the minimum value one approach is that you search for the maximum element and then replace the maximum element with the element in the first position of the unsorted array. For this you need to use the correct relational operators and do the correct comparison of the operators just that so that you select the maximum element and then swap it with the element in the first position to sort the elements in the reverse chronological order. These are the references. Thank you for watching.