Selection Sort - Variants

Variants

Heapsort greatly improves the basic algorithm by using an implicit heap data structure to speed up finding and removing the lowest datum. If implemented correctly, the heap will allow finding the next lowest element in Θ(log n) time instead of Θ(n) for the inner loop in normal selection sort, reducing the total running time to Θ(n log n).

A bidirectional variant of selection sort, called cocktail sort, is an algorithm which finds both the minimum and maximum values in the list in every pass. This reduces the number of scans of the list by a factor of 2, eliminating some loop overhead but not actually decreasing the number of comparisons or swaps. Note, however, that cocktail sort more often refers to a bidirectional variant of bubble sort.

Selection sort can be implemented as a stable sort. If, rather than swapping in step 2, the minimum value is inserted into the first position (that is, all intervening items moved down), the algorithm is stable. However, this modification either requires a data structure that supports efficient insertions or deletions, such as a linked list, or it leads to performing Θ(n2) writes.

In the bingo sort variant, items are ordered by repeatedly looking through the remaining items to find the greatest value and moving all items with that value to their final location. Like counting sort, this is an efficient variant if there are many duplicate values. Indeed, selection sort does one pass through the remaining items for each item moved. Bingo sort does one pass for each value (not item): after an initial pass to find the biggest value, the next passes can move every item with that value to its final location while finding the next value as in the following pseudocode (arrays are zero-based and the for-loop includes both the top and bottom limits, as in Pascal):

bingo(array A) { This procedure sorts in ascending order. } begin max := length(A)-1; { The first iteration is written to look very similar to the subsequent ones, but without swaps. } nextValue := A; for i := max - 1 downto 0 do if A > nextValue then nextValue := A; while (max > 0) and (A = nextValue) do max := max - 1; while max > 0 do begin value := nextValue; nextValue := A; for i := max - 1 downto 0 do if A = value then begin swap(A, A); max := max - 1; end else if A > nextValue then nextValue := A; while (max > 0) and (A = nextValue) do max := max - 1; end; end;

Thus if on average there are more than two items with each value, bingo sort can be expected to be faster, because it executes the inner loop fewer times than selection sort.

Read more about this topic:  Selection Sort

Famous quotes containing the word variants:

    Nationalist pride, like other variants of pride, can be a substitute for self-respect.
    Eric Hoffer (1902–1983)