 Hello everyone, I am Mrs. Meenakshi Sri Gandhi, Assistant Professor from Walsh Chandra Institute of Technology, Sholapur. Welcome to the video lecture on Selection Sort. Learning outcome. At the end of this session, students will be able to implement Selection Sort using C-Programming. Sorting. Let us see what is sorting is. Sorting takes an unordered collection of elements and makes it ordered one. For example, here an unsorted array is taken as an input and sorting makes it an ordered one. So, a sorting is nothing but to make an unsorted array into a sorted one. Let us see what is Selection Sort is. Selection Sort is an in place comparison based algorithm. In this, the list is divided into two parts. The sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. In this algorithm, the smallest element is selected from the unsorted array and is formed with the leftmost element and that element becomes a part of the sorted array. This process continues till the entire list is sorted. So, in general, this algorithm selects the smallest element from the list and place it at the first position. Then it selects the second smallest element and place it at the second position and this process goes on till the entire list is sorted. Let us see the illustration of Selection Sort. For that, we consider an array A with 6 elements that are 52, 67, 34, 24, 86 and 46. Now, these array elements are placed in the array where the array starts with the index 0 and it ends at the index with size minus 1. Now, in the Selection Sort, we are taking a variable called as min. Minimum index is the variable which will hold the index of the smallest element. So, at the very beginning, we do not know which is the smallest element. So, we assume that the minimum index is equal to 0. So, in the first iteration, here we are comparing the A of 1 element with A of min. We are comparing whether A of 1 is lesser than A of min or not. In this case, we see that 67 is greater than A of min. So, here the minimum index remains as it is, that is, it remains at 0 only. After that, the comparison is taking place between the A of second element and A of min. Here we see that A of 2 is lesser than A of min. That is why we see that the minimum index is now selected as 2. Next, now the comparison is taking place between A of 3 and A of min. Here we have checked that A of 3 is lesser than A of min and so the minimum index is now changed to 3. So, with the new minimum index with value 3, the comparison is now taking place between A of 4 and A of min. Here A of 4 is checked whether it is lesser than A of min or not. As it is greater, so minimum index remains as it is, that it remains at equal to 3 only. Next, the comparison is taking place with the index value at 3 and 5. So, here we see that A of 5 is greater than A of min. That is why the minimum index remains at 3 only. So, here we find that the smallest element is placed at the index 3. Now, the smallest element should be swapped with the first element. So, after swapping, you can see that the element, the first smallest element has been sorted at its proper place. So, after the end of the first iteration, we can see that the smallest element is sorted. In the second iteration, we now do the comparison from the index 1 onwards because here we see that the first element has been already sorted. So, we do not take this element for any comparison. Now, assuming minimum index equal to 1, the comparison goes on. So, here the first two elements are compared and here we can see that A of 2 is lesser than A of min. That is why the minimum index now changes with value 2. With the new minimum index that is equal to 2, the comparison is taken with the index values at 2, 1, 3. A of 3 as it is greater than A of min, the minimum index value remains as it is that is equal to 2. Then the comparison is taking place between A of 2 and A of 4. That is as A of 4 is greater than A of min, the minimum index will remain as it is. Next, the comparison is taking place between these two elements A of 5 and here we see that A of 5 is greater than A of min. That is why the minimum index value remains as it is that is equal to 2. Now, after this we can see that the second smallest element is there at the index 2. Now, this element should be swapped with the second element. So, after swapping, we can see that the element is placed at the second position of the array. So, this is the end of the second iteration and we can see that the second smallest element is also sorted. In the third iteration, the first two elements are already sorted. So, we do not take these two elements in comparison and the minimum index value is now initialized to the index 2 because this is the first element for unsorted array. So, the comparison is taken between these two elements and here we see that A of 3 is lesser than A of min. That is why the minimum index value is now changed to 3. With the new minimum index that is equal to 3, the comparison is taking place between these two elements and here we see that A of 4 is greater than A of min. That is why the minimum index remain as it is that is equal to 3. After this, the comparison is taking place between these two elements. Here we see that A of 5 is lesser than A of min. That is why the minimum index value is now changed to 5. So, here we see that the next smallest element is there at this index. Now this is the third smallest element and this element should be swapped with the third element of the array. So, after swapping we can see that this is the end of the third iteration and the third smallest element is also sorted. In the fourth iteration, the minimum index value is now set to 3 and the comparison is taken place between these two elements. As A of 4 is greater than A of min, the minimum index remain as it is that is equal to 3. Again the next comparison is taking place between these two elements. Here again we see that A of 5 is greater than A of min. That is why the minimum index value remain as it is that is equal to 3. So the next smallest element is there at the index 3. Now this element should be swapped but as it is at the same position so it is not swapped here so it remain as it is. So at the end of the fourth iteration we can see that the fourth smallest element is also sorted. In the next iteration, the minimum index is set at index 4 and the comparison is taken place between these two elements. Here we see that A of 5 is lesser than A of min that is why the minimum index value is set with the new value that is equal to 5. And we get the next smallest element that is placed at the index 5. This should be swapped with the fifth element of the array. So after swapping we can see that after this is the end of the fifth iteration and the fifth element is also sorted. So here all the elements are sorted. So this is how the selection sort works. It finds the smallest element every time and it is placed at its proper position. Let us see the time complexity of selection sort. In the best case the time complexity of selection sort is o n square and in the worst case and in the average case the time complexity of selection sort is again o n square. Try to think and answer what is the algorithm for selection sort. Pause the video for some time and note down the answer in your book. Let us see the algorithm for selection sort. At the very beginning we are taking then input from the user, we are taking an input into an array of n elements and we are initializing i equal to 0 and temp equal to 0. Then we need to repeat the steps from 4 to 8 while i is lesser than n minus 1. And in each iteration we need to set minimum index equal to i and set j equal to i plus 1. We need to repeat step 7 while j is lesser than 9. So in step 7 we are comparing a of jth element and a of minimum index element. If a of j is lesser than a of minimum index then we are changing the minimum index equal to j. Minimum index is not equal to i. In that case we are swapping the minimum index and the ith element. So your temp is set to a of ith element and a of i is set to a of minimum index element and a of minimum index is set to temp. So this is the algorithm for selection sort. Let us see the implementation of selection sort using C programming. So first in the main function we are declaring all the integer variable. We are declaring the array then the temp and the minimum index. After that we are asking the user for how many elements he wants to enter in the array. And that is taken into n. And by using the for loop we are taking n elements from the user and these are entered into the array. i is initialized to 0 and we are using a while and the condition is i is lesser than n minus 1. Till i is lesser than n minus 1 the while loop repeats and in each iteration the minimum index is set to i value and j is equal to i plus 1. Now in each iteration here we are comparing the jth element with the minimum index element. If a of j is lesser than a of minimum index then minimum index is now set to the jth value and j is incremented with each iteration. This loop repeats till all the elements are tested and minimum index will contain the index of the smallest element. Once we come out of this loop we are checking whether the minimum index whether it is equal to i or not. If it is not equal to i then we need to swap the elements which are placed at minimum index and i. So for that we are setting temp equal to the array of i and array of i is set with array of minimum index and array of minimum index is set with temp value. So here we have swapped the values which are placed at the index i and at the index minimum index and after swapping i is incremented by 1 so that it will go for the next iteration. So this loop terminates till i is lesser than n minus 1. Once it comes out of the loop all the elements of the array are sorted and after that we need to print the elements of the array and we are using the for loop which will start from the index 0 and it will terminate till it reaches to n minus 1. So at in each iteration the array element is displayed here. So this was the implementation of selection sort using C programming. So these are my references. Thank you.