Selection sort
Selection sort is designed to make the fewest swaps possible. For a vector of n elements selection sort will make n swaps. Here’s how it works:
- scan the unsorted portion of the vector and select the smallest element
- swap that element with the leftmost unsorted element
- repeat until there are no more unsorted elements
Selection sort has time complexity of \mathcal{O}(n^2) and space complexity of \mathcal{O}(1).
In this video, we implement selection sort in C++.
Additional resources:
Comprehension check:
- Writing to a memory location takes more time than _______________ from a memory location.
- True or false? Selection sort is not stable.
- Given a vector of n elements, selection sort will make _____________ swaps to completely sort the vector.
- If we have a vector of 10 elements, and three elements are sorted, selection sort will perform _______ comparisons in order to sort the next element.
- True or false? Given the vector 0, 3, 4, 7, 1, 2, after the first iteration, the vector will be [0, 1, 4, 7, 3, 2.]
[Answers: ǝnɹʇ / 9 / u / ǝnɹʇ / ƃuᴉpɐǝɹ]
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.