Selection Sort
This page provides a brief introduction to selection sort.
Definition
Selection sort is a simple and intuitive sorting algorithm. Its working principle is to find the \(i\)-th smallest element (i.e., the smallest element in \(A_{i..n}\)) each time, and then swap this element with the element at the \(i\)-th position in the array.
Properties
Stability
The stability of selection sort depends on its specific implementation.
If implemented using a linked list, since insertion and deletion at any position in a linked list are \(O(1)\), there is no need to use a swap (exchange two elements) operation: each time the smallest element is selected from the unsorted part (if there are multiple, pick the first one), it is inserted before the first element of the unsorted part, which ensures stability.
If implemented using an array (the common implementation in OI), since insertion and deletion at any position in an array are \(O(n)\), only swap can be used to move elements from the unsorted part to the sorted part. The swap operation makes the array-based implementation of selection sort unstable.
The implementation examples given below are all based on swapping array elements, and therefore are unstable.
Time Complexity
The best-case, average-case, and worst-case time complexities of selection sort are all \(O(n^2)\).
Code Implementation
Pseudocode
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用