making only one exchange for every pass through the list. This algorithm will first find the smallest elements in array and swap it with the element in the first position of an array, then it will find the second smallest element and swap that element with the element in the second position, and it will continue until the entire array is sorted in respective order. This algorithm repeatedly selects the next-smallest element and swaps in into the right place for every pass. Hence it is called selection sort.
Procedure . Start from the first element i.e., index- , we search the smallest element in the array, and replace it with the element in the first position. 12th Computer Chapter - - . Now we move on to the second element position, and look for smallest element present in the sub-array, from starting index to till the last index of sub - array.
. Now replace the second smallest identified in step- at the second position in the or original array, or also called first position in the sub array. . This is repeated, until the array is completely sorted.
Let's consider an array with values { , , , , , } Below, we have a pictorial representation of how selection sort will sort the given array. Initial array At the end First pass At the end Fifth pass At the end Second pass At the end Third pass At the end Fourth pass In the first pass, the smallest element will be , so it will be placed at the first position. After that, next smallest element will be searched from an array. Now we will get as the smallest, so it will be then placed at the second position.
Then leaving the first element, next smallest element will be searched, from the remaining elements. We will get as