How it works?
Lesson 3 of 9 in Coddy's Selection Sort - DSA Series course.
Imagine you have a row of numbered cards on a table, and you want to arrange them from smallest to largest.
Step-by-Step Process:
- Find the smallest: scan the whole row and find the smallest card.
- Swap it to the front: swap that card with the card in the first position. Now the first card is in its final place.
- Repeat for the rest: look at the remaining cards (from the second position onward), find the smallest among them, and swap it into the second position.
- Keep going until every position holds its correct card.
Example on [64, 25, 12, 22, 11]:
- Smallest is 11, swap with 64: [11, 25, 12, 22, 64]
- Smallest of the rest is 12, swap with 25: [11, 12, 25, 22, 64]
- Smallest of the rest is 22, swap with 25: [11, 12, 22, 25, 64]
- Smallest of the rest is 25, already in place: [11, 12, 22, 25, 64]
- Only 64 remains, already in place. Sorted!
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Selection Sort - DSA Series
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)