Selection Sort
excerpt: How does selectionsort work? What is the time complexity? When can we use selectionsort? tags: swap nestedforloops reducingthedomainsofthevariables
The goal is to order a list of integers in ascending order. The selectionsort is quadratic in time complexity but simple. It can outperform faster but more complicated algorithms for very small arrays.
Selectionsort in Arrays
The basic idea is to search the input list for the largest item and then add it to the end of a growing sorted list. An alternative approach is to find the smallest item and move it to the beginning of the growing sorted list. The steps to implement the selectionsort:
 Iterate through the array from index 0 to array size  1.
 Find the item that must be in position i.
 Find the smallest item with index j >= i.
 Swap values[i] and values[j]
Iterate through the array to find the smallest element that has not yet been added to the sorted part of the array. Then swap the smallest item with the item in position i.
The first image shows the unsorted array. The second image shows the first three items that has been sorted. The code is getting ready to swap th enext element into position. The code searches the unsorted elements to find the one with the smallest value (3). Then this item is swapped into the next unsorted position. The last image shows the array after the new element has been moved to the sorted part of the array. The for loop continues to the next step to add the next element (5) to the growing sorted portion of the array.
The items are sorted inplace so no additional space is used. If the array contains N elements, each of the N positions must be considered. For each position i, we must seach N  i elements that have not yet been sorted to find the element that must be in position i. The code then swaps the item into its final position in a small constant number of steps.
The total run time is calculated by adding up all the steps required to move all the elements.
(N1) + (N2) + ... + 2 + 1 = (N<sup>2</sup> + N)/2
Therefore, the run time is O(N2). This is fast enough for small arrays of size 5 to 10 elements or so. This algorithm is relatively simpler that complicated algorithms that are faster.
Alternative Implementation
Consider the list as if it was divided into two parts, one sorted and the other unsorted. (note: at the beginning the sorted part is empty).
 Select the smallest element in the unsorted part of the list
 Swap that element with the element in the initial position of the unsorted array
 Change where you divide the array from the sorted part to the unsorted part.


Building Blocks
The selection sort algorithm uses the following basic building blocks:
 Nested For Loop
 Reducing the Domains of the Variables
Recursive Implementation

