Selection Sort

Selection Sort is a simple $$O(n^2)$$ algorithm using $$O(1)$$ memory. it works by repeatedly "selecting" the minimum element and putting it in its right position.

Selection sort is not adaptive, meaning it doesn't take less time on almost-sorted arrays than random ones. It is also unstable, meaning it does not preserve the relative order of equal elements.

Although Selection Sort is an $$O(n^2)$$ algorithm, it makes $$O(n)$$ element moves, making it useful for Block Merge Sorts and other algorithms that require few data moves.

Algorithm
First, the sort does a linear scan of the input to find the minimum. Then it swaps it with the beginning of the array. Now the sort does another linear scan to determine the second-smallest item, and swaps it with the second element, and so on until all elements are swapped out in order.

Example
Selection Sort the list. Select the minimum and swap:

[3 4 2 1]       ↑ [1 4 2 3]  └─────┘

Select and swap again:

[1 4 2 3]     ↑ [1 2 4 3]    └─┘

Repeat until sorted:

[1 2 4 3]       ↑ [1 2 3 4]      └─┘

Pseudocode
procedure selection_sort(array arr) do for index = 0 to length(arr)-1 do int min = index; int i = index; for j = index+1 to length(arr)-1 do if arr[min] > arr[j] min = j            end if end for swap arr[min] and arr[i] end for end

Double Selection
Instead of searching for only the min/max item each iteration, Double Selection searches for both the min and max and swaps them to the left and right ends of the array respectively. This algorithm can save a small amount of comparisons since an element found to be the min cannot be the max, however this improvement is very small and doesn't affect the constant on the average number of comparisons.

Double Selection also introduces some edge cases in which the implementer needs to be aware of. It's possible for the max to be on the left side of the array where the min is supposed to be swapped to and vice versa. By swapping the min to the left, the max item is displaced and needs to be handled accordingly by the algorithm.

Stable Selection
Stable Selection is the stable variant of Selection. The original algorithm is unstable due to swapping elements across large distances which can swap equal elements over each other. Since the elements are all equal in value, it's impossible to tell which of them comes first.

To solve this problem, Stable Selection inserts the minimum to the start of the array by swapping pairs of elements repeatedly instead of swapping once over a long distance. This ensures that the elements the minimum traveled across maintain their order and guarantees each iteration stable. However, Stable Selection now makes $$O(n^2)$$ moves instead of $$O(n)$$ moves.

Another method is to force Selection Sort stable by assigning each element a unique value. This way, the algorithm can now tell which equal element comes first. This also keeps Stable Selection at $$O(n)$$ moves but introduces $$O(n)$$ space.

Bingo Sort
Bingo Sort is a variant of Selection Sort that swaps equal elements into their correct place. it does so by finding the minimum, and then scanning through the array again to swap any elements that are equal to it to the front. it is adaptive on few unique distributions, with a higher constant factor on distinct inputs.

Badsort

 * Not to be confused with Worstsort's "Badsort".

Badsort is a sorting algorithm found in Musicombo's videos. It is similar, but results in $$O(n^3)$$ worst case. The algorithm redefines finding the minimum by checking if the current element is greater than at least one of the elements after it. If so, advance the current element to the next one and check again, otherwise, that is the minimum.