HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, selection sort is an
in-place In computer science, an in-place algorithm is an algorithm which transforms input using no auxiliary data structure. However, a small amount of extra storage space is allowed for auxiliary variables. The input is usually overwritten by the output ...
comparison Comparison or comparing is the act of evaluating two or more things by determining the relevant, comparable characteristics of each thing, and then determining which characteristics of each are similar to the other, which are different, and t ...
sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. ...
. It has an O(''n''2)
time complexity In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
, which makes it inefficient on large lists, and generally performs worse than the similar
insertion sort Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Howev ...
. Selection sort is noted for its simplicity and has performance advantages over more complicated algorithms in certain situations, particularly where
auxiliary memory Computer data storage is a technology consisting of computer components and recording media that are used to retain digital data. It is a core function and fundamental component of computers. The central processing unit (CPU) of a computer ...
is limited. The algorithm divides the input list into two parts: a sorted sublist of items which is built up from left to right at the front (left) of the list and a sublist of the remaining unsorted items 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 (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. The time efficiency of selection sort is quadratic, so there are a number of sorting techniques which have better time complexity than selection sort.


Example

Here is an example of this sort algorithm sorting five elements: (Nothing appears changed on these last two lines because the last two numbers were already in order.) Selection sort can also be used on list structures that make add and remove efficient, such as a
linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whic ...
. In this case it is more common to ''remove'' the minimum element from the remainder of the list, and then ''insert'' it at the end of the values sorted so far. For example:
arr[] = 64 25 12 22 11

// Find the minimum element in arr[0...4]
// and place it at beginning
11 25 12 22 64

// Find the minimum element in arr[1...4]
// and place it at beginning of arr[1...4]
11 12 25 22 64

// Find the minimum element in arr ...4// and place it at beginning of arr ...411 12 22 25 64

// Find the minimum element in arr ...4// and place it at beginning of arr ...411 12 22 25 64 


Implementations

Below is an implementation in C. /* a to a Length-1is the array to sort */ int i,j; int aLength; // initialise to a's length /* advance the position through the entire array */ /* (could do i < aLength-1 because single element is also min element) */ for (i = 0; i < aLength-1; i++)


Complexity

Selection sort is not difficult to analyze compared to other sorting algorithms, since none of the loops depend on the data in the array. Selecting the minimum requires scanning n elements (taking n-1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n-2 elements and so on. Therefore, the total number of comparisons is : (n-1)+(n-2)+...+1 = \sum_^i By
arithmetic progression An arithmetic progression or arithmetic sequence () is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression with a common differ ...
, : \sum_^i= \frac(n-1)= \fracn(n-1)= \frac(n^2-n) which is of complexity O(n^2) in terms of number of comparisons. Each of these scans requires one swap for n-1 elements (the final element is already in place).


Comparison to other sorting algorithms

Among quadratic sorting algorithms (sorting algorithms with a simple average-case of Θ(''n''2)), selection sort almost always outperforms
bubble sort Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed. These passes ...
and gnome sort.
Insertion sort Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. Howev ...
is very similar in that after the ''k''th iteration, the first k elements in the array are in sorted order. Insertion sort's advantage is that it only scans as many elements as it needs in order to place the k+1st element, while selection sort must scan all remaining elements to find the k+1st element. Simple calculation shows that insertion sort will therefore usually perform about half as many comparisons as selection sort, although it can perform just as many or far fewer depending on the order the array was in prior to sorting. It can be seen as an advantage for some
real-time Real-time or real time describes various operations in computing or other processes that must guarantee response times within a specified time (deadline), usually a relatively short time. A real-time process is generally one that happens in defined ...
applications that selection sort will perform identically regardless of the order of the array, while insertion sort's running time can vary considerably. However, this is more often an advantage for insertion sort in that it runs much more efficiently if the array is already sorted or "close to sorted." While selection sort is preferable to insertion sort in terms of number of writes (n-1 swaps versus up to n(n-1)/2 swaps, with each swap being two writes), this is roughly twice the theoretical minimum achieved by
cycle sort Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the pe ...
, which performs at most ''n'' writes. This can be important if writes are significantly more expensive than reads, such as with
EEPROM EEPROM (also called E2PROM) stands for electrically erasable programmable read-only memory and is a type of non-volatile memory used in computers, usually integrated in microcontrollers such as smart cards and remote keyless systems, or as a ...
or
Flash memory Flash memory is an electronic non-volatile computer memory storage medium that can be electrically erased and reprogrammed. The two main types of flash memory, NOR flash and NAND flash, are named for the NOR and NAND logic gates. Both us ...
, where every write lessens the lifespan of the memory. Selection sort can be implemented without unpredictable branches for the benefit of CPU
branch predictor In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g., an if–then–else structure) will go before this is known definitively. The purpose of the branch predictor is to improve the flow ...
s, by finding the location of the minimum with branch-free code and then performing the swap unconditionally. Finally, selection sort is greatly outperformed on larger arrays by \Theta(n\log n)
divide-and-conquer algorithms Divide and rule policy ( la, divide et impera), or divide and conquer, in politics and sociology is gaining and maintaining Power (social and political), power divisively. Historically, this strategy was used in many different ways by empire ...
such as
mergesort In computer science, merge sort (also commonly spelled as mergesort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same i ...
. However, insertion sort or selection sort are both typically faster for small arrays (i.e. fewer than 10–20 elements). A useful optimization in practice for the recursive algorithms is to switch to insertion sort or selection sort for "small enough" sublists.


Variants

Heapsort In computer science, heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like selection sort, heapsort divides its input into a sorted and an unsorted region, and it iteratively shrinks the ...
greatly improves the basic algorithm by using an
implicit Implicit may refer to: Mathematics * Implicit function * Implicit function theorem * Implicit curve * Implicit surface * Implicit differential equation Other uses * Implicit assumption, in logic * Implicit-association test, in social psychology ...
heap Heap or HEAP may refer to: Computing and mathematics * Heap (data structure), a data structure commonly used to implement a priority queue * Heap (mathematics), a generalization of a group * Heap (programming) (or free store), an area of memory f ...
data structure In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, a ...
to speed up finding and removing the lowest datum. If implemented correctly, the heap will allow finding the next lowest element in \Theta(\log n) time instead of \Theta(n) for the inner loop in normal selection sort, reducing the total running time to \Theta(n\log n). A bidirectional variant of selection sort (called double selection sort or sometimes cocktail sort due to its similarity to
cocktail shaker sort Cocktail shaker sort, also known as bidirectional bubble sort, cocktail sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, or shuttle sort, is an extension of bubble sort. The algorithm extends bub ...
) finds ''both'' the minimum and maximum values in the list in every pass. This requires three comparisons per two items (a pair of elements is compared, then the greater is compared to the maximum and the lesser is compared to the minimum) rather than regular selection sort's one comparison per item, but requires only half as many passes, a net 25% savings. Selection sort can be implemented as a
stable sort In computer science, a sorting algorithm is an algorithm that puts elements of a list into an order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. Efficient sorting is important ...
if, rather than swapping in step 2, the minimum value is ''inserted'' into the first position and the intervening values shifted up. However, this modification either requires a data structure that supports efficient insertions or deletions, such as a linked list, or it leads to performing \Theta(n^) writes. In the bingo sort variant, items are sorted 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 In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small positive integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that possess di ...
, this is an efficient variant if there are many duplicate values: selection sort does one pass through the remaining items for each ''item'' moved, while Bingo sort does one pass for each ''value''. After an initial pass to find the greatest value, subsequent passes move every item with that value to its final location while finding the next value as in the following
pseudocode In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
(arrays are zero-based and the for-loop includes both the top and bottom limits, as in
Pascal Pascal, Pascal's or PASCAL may refer to: People and fictional characters * Pascal (given name), including a list of people with the name * Pascal (surname), including a list of people and fictional characters with the name ** Blaise Pascal, Fren ...
): bingo(array A) begin last := length(A) - 1; nextMax := A ast for i := last - 1 downto 0 do if A > nextMax then nextMax := A while (last > 0) and (A ast= nextMax) do last := last - 1; while last > 0 do begin prevMax := nextMax; nextMax := A ast for i := last - 1 downto 0 do if A > nextMax then if A <> prevMax then nextMax := A else begin swap(A A ast; last := last - 1; end while (last > 0) and (A ast= nextMax) do last := last - 1; end; end; Thus, if on average there are more than two items with the same value, bingo sort can be expected to be faster because it executes the inner loop fewer times than selection sort.


See also

*
Selection algorithm In computer science, a selection algorithm is an algorithm for finding the ''k''th smallest number in a list or array; such a number is called the ''k''th ''order statistic''. This includes the cases of finding the minimum, maximum, and median el ...


References

*
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of computer sc ...
. ''
The Art of Computer Programming ''The Art of Computer Programming'' (''TAOCP'') is a comprehensive monograph written by the computer scientist Donald Knuth presenting programming algorithms and their analysis. Volumes 1–5 are intended to represent the central core of compu ...
'', Volume 3: ''Sorting and Searching'', Third Edition. Addison–Wesley, 1997. . Pages 138–141 of Section 5.2.3: Sorting by Selection. * Anany Levitin. ''Introduction to the Design & Analysis of Algorithms'', 2nd Edition. . Section 3.1: Selection Sort, pp 98–100. * Robert Sedgewick. ''Algorithms in C++, Parts 1–4: Fundamentals, Data Structure, Sorting, Searching: Fundamentals, Data Structures, Sorting, Searching Pts. 1–4'', Second Edition. Addison–Wesley Longman, 1998. . Pages 273–274


External links

* – graphical demonstration {{sorting Sorting algorithms Comparison sorts Articles with example C code