Selection Sort
What is Selection Sort?
Selection sort is a sorting algorithm that sorts a collection of elements by selecting the smallest element in the collection and then swapping it with the first element in the collection.
How to implement Selection Sort?
To implement selection sort, we need to select the smallest element in the collection and then swap it with the first element in the collection.
Example
Let's sort the collection [3, 2, 4, 1, 2, 1] using selection sort.
First, we need to select the smallest element in the collection and then swap it with the first element in the collection.
Element | Selection | Swap |
---|---|---|
3 | 1 | 1 |
2 | 1 | 1 |
4 | 1 | 1 |
1 | 1 | 1 |
2 | 1 | 1 |
1 |
The sorted collection is [1, 1, 2, 2, 3, 4].
Become a Sponsor![Heart with ribbon](/_next/image?url=%2Fheart-with-ribbon.png&w=48&q=75)
Become a sponsor and help us maintain and improve this project. Every contribution counts. Thank you!
GitHub Sponsors