selection sort
The Problem
See the Sorting Problem.
The Algorithm
Suppose is the initial list of unsorted elements.The selection sort algorithm
sorts this list in steps. At each step , find the largestelement such that , and swap it with the element at .So, for the first step, find the largest value in the list and swap it with the last element in the list.For the second step, find the largest value in the list up to (but not including) the last element, and swap itwith the next to last element. This is continued for steps. Thus the selection sort algorithm is avery simple, in-place sorting algorithm.
Pseudocode
Algorithm Selection_Sort(L, n)
Input: A list of elements
Output: The list in sorted order
begin
for do
begin
for do
if then
end
end
Analysis
The selection sort algorithm has the same runtime for any set of elements, no matterwhat the values or order of those elements are. Finding the maximum element of a list of elements requires comparisons. Thus , the number of comparisons required tosort a list of elements with the selection sort, can be found:
However, the number of data movements is the number of swaps required, which is . Thisalgorithm is very similar to the insertion sort algorithm. It requires fewer data movements,but requires more comparisons.