Friday 28 February 2014

Selection Sort

Selection Sort
Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O (n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.
Example
Here is an example of this sort algorithm sorting five elements:
64 25 12 22 11
11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
11 12 22 25 64
Algorithm
for i = 1:n,
    k = i
    for j = i+1:n, if a[j] < a[k], k = j
    ? invariant: a[k] smallest of a[i..n]
    swap a[i,k]
    ? invariant: a[1..i] in final position

end

No comments:

Post a Comment