Selection Sort (Algo) :

Selection sort is a sorting algorithm that has quadratic running time complexity given as O(n^2) thereby making it inefficient to be used on large lists.Selection sort is generally the preferred choice for sorting files with very large objects and small keys.

The algorithm for selection is shown in figure.The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.*

SMALLEST(ARR, K, N, POS)
Step I :     [Initialize] SET SMALL = ARR[K]
Step II :    [Initialize] SET POS = K
Step III :  Repeat for J=k+1 to N
 IF SMALL > ARR[J] , Then
 SET SMALL = ARR[J]
SET POS = J
[End of if ]
[ End of Loop ]
Step IV : Exit.

SELECTION SORT() :
Step I :     Repeat Steps 2 and 3 for K = 1 to N-1
Step II :   Call SMALLEST(ARR, K, N, POS)
Step III:   SWAP ARR[K] with ARR[POS]
[ End of Loop]
Step IV :   Exit.

Figure :-
SS

Leave a comment