Selection Sort Algorithm Example | DSA Tutorials YASH PAL, 8 May 20268 May 2026 The selection sort algorithm has a complexity of O(n2). The advantage of selection sort is that it is simple and easy to implement. The disadvantage is that it is inefficient for large lists, so, similar to the more efficient insertion sort, the insertion sort should be used in its place.Selection Sorting AlgorithmThe selection sorting is based on the idea of selecting the smallest unsorted item remaining in the list and then swapping it with the item to be replaced in the next position. The logic for selection sort is very simple.Sorting StepsStep-1: Given a set of elements N, we find out the location of the minimum element among the set and replace it with the location of the first element.Step-2: Next, we do the same task for N-1 elements and find out the location of the minimum element among N – 1 elements and replace it with the location of the first element (of set N – 1). After the first step, the minimum element will be at the first location. After the second step, the second minimum element among the remaining list will be at the second position and soon.Step-3: Next, we repeat the same task for all the elements and find out the location of the minimum element among the remaining array elements and replace it with the respective position.Step (N-1): This process is repeated, and finally, after N – 1 steps, the list or set will be sorted. Here, N is the number of elements of the array.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 smallest 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 ExampleLet’s take an example to understand the selection sort in detail with a complete step-by-step process.Example 1: We have an array ‘A‘ with ‘N‘ elements.2 5 6 7 8 12 34 20 1 67Solution: So, using the selection sort, we need N-1 = 10 – 1 = 9 steps to sort the array.Lmin = smallest element indexA[0] = 2A[1] = 5A[2] = 6A[3] = 7A[4] = 8A[5] = 12A[6] = 34A[7] = 20A[8] = 1A[9] = 67Step 1: First, we are going to find the Index of the smallest element, which is Lmin = A[8] = 1. So we are going to swap the position of A[8] to A[0] (means the value of A[8] goes to the first position and the value of A[0] goest ot 8th position of the array). So our new array is:2, 5, 6, 7, 8, 12, 34, 20, 1, 67 ⇒\Rightarrow 1, 5, 6, 7, 8, 12, 34, 20, 2, 67Now, the first position of the array has the smallest element. So we are going to follow the same step with the remaining array elements. First, we are going to find the smallest element in the remaining array and then swap the position of that element with the 2nd position of the array. In this selection sorting algorithm, we need 9 steps to sort the array. All the sorting steps are shown in Table 1 below.StepsElements in the arrayRemarks12 5 6 7 8 12 34 20 1 67Lmin = 8, swap A[0] ⇔\Leftrightarrow A[8]21 5 6 7 8 12 34 20 3 67Lmin = 8, swap A[1] ⇔\Leftrightarrow A[8]31 2 6 7 8 12 34 20 5 67Lmin = 8, swap A[2] ⇔\Leftrightarrow A[8]41 2 5 7 8 12 34 20 6 67Lmin = 8, swap A[3] ⇔\Leftrightarrow A[8]51 2 5 6 8 12 34 20 7 67Lmin = 8, swap A[4] ⇔\Leftrightarrow A[8]61 2 5 6 7 12 34 20 8 67Lmin = 8, swap A[5] ⇔\Leftrightarrow A[8]71 2 5 6 7 8 34 20 12 67Lmin = 8, swap A[6] ⇔\Leftrightarrow A[8]81 2 5 6 7 8 12 20 34 67Lmin = 7, swap A[7] ⇔\Leftrightarrow A[7]91 2 5 6 7 8 12 20 34 67Lmin = 8, swap A[8] ⇔\Leftrightarrow A[8]1 2 5 6 7 8 12 20 34 67The array is now sortedTable 1: Selection sorting algorithm exampleAlgorithm StepsThe algorithm for selection sort is very simple:selsort (arr[], N) { for (i=0 to N-1) { loc = location of the minimum element in the index range 0 to N-i-1 swap arr[i] and arr[loc] } }selsort (arr[], N) { for (i=0 to N-1) { loc = location of the minimum element in the index range 0 to N-i-1 swap arr[i] and arr[loc] } }Algorithm ImplementationSELECTION_SORT (A, N) 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[i] 3. Set POS: = i 4. Repeat Steps for j = i + 1 to N: If A[j] < MIN, then: (a) Set MIN:= A[j] (b) Set POS:= j [End of If structure]SELECTION_SORT (A, N) 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[i] 3. Set POS: = i 4. Repeat Steps for j = i + 1 to N: If A[j] < MIN, then: (a) Set MIN:= A[j] (b) Set POS:= j [End of If structure]Let’s take another example to understand the selection sort, and then we implement the algorithm in a programming language.Example 2: Suppose an array A contains 8 elements as follows:77, 33, 44, 11, 88, 22, 66, 55Applying the selection sort algorithm to A yields the data. Observe that LOC gives the location of the smallest among A[K], A[K+1], …, A[N] during Pass K. The green elements indicate the elements that are to be interchanged.Pass12345678A[1]A[2]A[3]A[4]A[5]A[6]A[7]A[8]K = 1LOC = 47733441188226655K = 2LOC = 61133447788226655K = 3LOC = 61122447788336655K = 4LOC = 61122337788446655K = 5LOC = 81122334488776655K = 6LOC = 71122334455776688K = 7LOC = 71122334455667788Sorted:1122334455667788Table 2: Selection soft for n = 8 itemsComplexity of Selection SortWorst caseBest caseAverage caseO(n2)O(n2)O(n2)Table 3: Complexity of selection sortData Structures & Algorithms Tutorials for Beginners Data Structures Tutorials DSA Tutorials