Selection Sort is an algorithm that sorts an array of numbers in non-decreasing order in time and space.
Tip
This problem can also be solved by Insertion Sort in time and space.
Tip
This problem can also be solved by Bubble Sort in time and space.
Tip
This problem can also be solved by Merge Sort in time and space.
Tip
This problem can also be solved by Heapsort in time and space.
Tip
This problem can also be solved by Quicksort in time and space.
Tip
This problem can also be solved by Quickselect in time and space.
Algorithm
- Find the minimum element among .
- Swap and , and sort recursively.
This algorithm solves the problem in time and space.
void selection_sort(int n, std::vector<int> &a) {
for (int i = 0; i < n - 1; i++) {
std::swap(a[i], *std::ranges::min_element(a | std::views::drop(i)));
}
}