selection sorting algorithm in data structures and algorithms
November 26, 2022

Selection Sorting Algorithm

Selection Sorting Algorithm: The selection sort starts from the first element and searches the entire list until it finds the minimum value. The sort places the minimum value in the first place, selects the second element, and searches for the second smallest element. The process continues until the complete list is sorted. A selection sort is one in which successive elements are selected in order and placed into their proper sorted positions.

The selection process must be done only from 1 to n – 1 rather than up to n. Analysis of the selection is so straightforward. The first pass makes n – 1 comparison, the second pass makes n – 2, and so on Such total comparisons are (N – 1) + (N2) (NE -3) ++ 2 + 1 = N (N – 1) / 2 = 0 (N³) The little additional storage required (except to hold few temporary variables.). The sort may therefore be categorized as O (N³), although it is faster than the bubble sort there is no improvement if the input file is completely sorted or unsorted.

Despite the fact that it is simple to code, it is unlikely that the straight selection sort would be used on any files but those for which n is small. The selection sort algorithm for sorting works as follows. First, find the smallest element in the list and put it in the first position. Then find the second smaller element in the list and put it in the second position. It is to be noted that the number of comparisons in the selection sort algorithm is independent of the original sort of the elements. The selection sort method requires (n – 1) passes to sort an array.

selection sorting algorithm

Selection Sorting Algorithm

Here A is an array with N elements. This algorithm sorts the array A with N elements in ascending order.

1. Repeat Steps 2 to 5 for I = 1 to N – 1:
2.       Set MIN : = A [ U ]
3.       Set POS : = I.
4.       Repeat Steps for J = 1 + 1 to N:
                 If A [ J ] < MIN , then :
                      (a) Set MIN : = A [ J ].
                      (b) Set POS : = J.
                 [ End of If structure. ]
           [ End of Step 4 loop |
           If I != POS, then
                (a) Set TEMP : = A [ I ]
                (b) Set A [ I ] = A [ POS ]
                (c) Set A [ POS ] : = TEMP
           [ End of If structure. ]
     [ End of step 1 loop 1 ]