Selection sort is an unstable, in-place sorting algorithm. The standard implementation is unstable but it can be made stable with a few modifications.
选择排序是一种不稳定的就地排序算法。 标准实现是不稳定的,但是可以通过一些修改使其变得稳定。
A stable sorting algorithm is the one where two keys having equal values appear in the same order in the sorted output array as it is present in the input unsorted array.
一种稳定的排序算法是一种具有相等值的键在排序后的输出数组中以与输入未排序数组中存在的键相同的顺序出现的算法 。
An in-place sorting algorithm has various definitions but a more used one is - An in-place sorting algorithm does not need extra space and uses the constant memory for manipulation of the input in-place. Although, it may require some extra constant space allowed for variables.
就地排序算法具有各种定义,但更常用的定义是-就地排序算法不需要额外的空间,并且使用常量内存来对就地输入进行操作。 虽然,它可能需要一些允许变量使用的额外常量空间。
It is an effective sorting algorithm with the worst time complexity of O(N^2) where N is the total number of elements. Selection sort functions by iteratively finding the smallest element and placing it at the start of the list. Thus, at the end of each iteration, the smallest element is placed at its current position in the sorted array.
这是一种有效的排序算法,具有最差的时间复杂度O(N ^ 2) ,其中N是元素总数。 选择排序通过迭代找到最小的元素并将其放置在列表的开头而起作用。 因此,在每次迭代结束时,最小的元素将被放置在排序数组中的当前位置。
Algorithm:
算法:
The algorithm maintains two sub-arrays. One sorted sub-array and another unsorted sub-array.
该算法维护两个子数组。 一个排序的子数组和另一个未排序的子数组。
Find the smallest element in the unsorted array.
Swap this element with the element at the front of the whole array thus shifting the first smallest element to its correct position in the sorted array
将该元素与整个数组前面的元素交换,从而将第一个最小的元素移动到其在已排序数组中的正确位置
Repeat steps 1 - 2 for the next n-1 elements.
对接下来的n-1个元素重复步骤1-2。
Pseudo-code:
伪代码:
1. for i
Example:
Input Array: 12 5 8 13
Iteration 1:
Minimum element in arr[0 - 3]: 5, shift it to the start of arr[0 - 3]
Array: 5 12 8 13
Iteration 2:
Minimum element in arr[1 - 3]: 8, shift it to the start of arr[1 - 3]
Array: 5 8 12 13
Iteration 3:
Minimum element in arr[2 - 3]: 12, shift it to the start of arr[2 - 3]
Array: 5 8 12 13
Last element gets sorted automatically.
Time Complexity: The time complexity of selection sort algorithm is
Worst case: O(N^2) comparisons but O(N) swaps
Average Case: Ɵ(N^2) comparisons but O(N) swaps
Best case: Ω(N^2) comparisons but no swaps
Space Complexity: Ɵ(1) constant
The best thing about this algorithm is that it always makes at most N swaps and hence, can be used when memory write is a costly operation.
Optimized Selection Sort Implementation:
Output:
Input Array:
12 8 5 10 13 9
Sorted Array:
5 8 9 10 12 13
Applications:
Better than Insertion sort in performance as the number of swaps is at most N in selection as compared to N^2 in insertion sort.
It can be used where writes are more expensive than reads, such as in EEPROM and Flash memory where the write operation lessens the lifespan of memory.
It is not preferred over bubble sort and gnome sort due to higher time complexity.
翻译自: https://www.includehelp.com/c-programs/implement-selection-sort-algorithm.aspx