Selection Sort
Last translate with upstream: ade5838(on Jul 14, 2021)
This article will briefly introduce selection sort.
Introduction¶
Selection sort is a simple and straightforward sorting algorithm. The principle is to find the
Properties¶
Stability¶
Because of the operation of swapping two elements, the selection sort is an unstable sort.
Time Complexity¶
The worst-case, average-case and best-case time complexity are all
Code Implementations¶
Pseudocode¶
C++ code¶
void selection_sort(int* a, int n) {
for (int i = 1; i < n; ++i) {
int ith = i;
for (int j = i + 1; j <= n; ++j) {
if (a[j] < a[ith]) {
ith = j;
}
}
int t = a[i];
a[i] = a[ith];
a[ith] = t;
}
}
Python¶
# Python Version
def selection_sort(a, n):
for i in range(1, n):
ith = i
for j in range(i + 1, n + 1):
if a[j] < a[ith]:
ith = j
a[i], a[ith] = a[ith], a[i]
buildLast update and/or translate time of this article,Check the history
editFound smelly bugs? Translation outdated? Wanna contribute with us? Edit this Page on Github
peopleContributor of this article OI-wiki
translateTranslator of this article Visit the original article!
copyrightThe article is available under CC BY-SA 4.0 & SATA ; additional terms may apply.