My little brother came home last week with five exam marks scribbled on a piece of paper — 29, 10, 14, 37, 13 — and a challenge: put them in order, fastest wins. He grabbed the paper and started swapping numbers at random, like most of us do. I stopped him. “There is a trick,” I told him, “and computers use it a million times a second.” That trick is called selection sort, and once you see it, you will never sort numbers the messy way again. It is the simplest sorting idea in all of programming, and it starts exactly the way you would sort a hand of playing cards.
📅 Want a study plan for this topic?
Get a free 7-day learning plan in your calendar — one small lesson every day.
What Is Selection Sort, Really?
Selection sort is a simple sorting algorithm: it looks at a list, finds the smallest item, and puts it first — then repeats the same thing for the rest. That is the whole secret. No clever shortcuts, no memory tricks, just one patient rule applied again and again. Programmers love it for one reason: it is the easiest sorting method to understand and to write. In Python it takes about five lines of code, which is why teachers reach for it first. The price? It is slow on big lists — sorting 10,000 items this way takes roughly 100 million comparisons, so nobody uses it for real apps. But as a first step into algorithms, honestly, nothing beats it.
The Main Idea
Most beginners get stuck right here: they think sorting means swapping things until the list "looks right." Selection sort never guesses. It works like this: you scan the whole unsorted part once, you find the smallest number in it, and you swap it into place. Done — that spot is finished forever. Then you move one step right and do it again. In my opinion, this is what makes it beautiful: every pass locks one number in its final home, so you can watch the sorted part grow from the left like a wall being built, brick by brick. The main idea is simple: find the smallest, put it first, repeat.
In Simple Words
Here is the whole idea, in four steps:
- Look at the unsorted part and find the smallest number.
- Swap it with the first number of the unsorted part.
- That spot is now sorted — never touch it again.
- Move one step right and repeat until the whole list is sorted.
Let's Solve an Example: Sorting Five Exam Marks
Meet Sara. Her five exam marks are [29, 10, 14, 37, 13], and she wants them from lowest to highest. Watch selection sort do its job, one pass at a time:
| Pass | What it does | Result |
|---|---|---|
| 1 | Finds the smallest (10) and swaps it with 29 | [10, 29, 14, 37, 13] |
| 2 | Finds the smallest of the rest (13) and swaps it with 29 | [10, 13, 14, 37, 29] |
| 3 | Finds the smallest of the rest (14) — already in place | [10, 13, 14, 37, 29] |
| 4 | Finds the smallest of the rest (29) and swaps it with 37 | [10, 13, 14, 29, 37] |
Notice pass 3, because this is the part most people miss: the smallest number was already in the right place, so selection sort did nothing — no swap, no fuss. The algorithm never assumes, it always checks. Four passes, four swaps at most, and a messy list of marks is perfectly sorted. If you compare it with bubble sort, which swaps neighbours again and again, selection sort is the calm one — fewer swaps, same result. And here is the honest truth about speed: with five numbers it feels instant, but with 1,000 numbers it checks about 500,000 pairs. That is why real apps use faster sorts — but they all learned the same lesson from this one. In my opinion, every beginner should sort a list by hand once. The moment you do, Big O notation stops being scary maths and becomes something you can feel.


Selection Sort vs Bubble Sort: What Is the Difference?
| Selection Sort | Bubble Sort | |
|---|---|---|
| How it works | Finds the smallest, swaps once per pass | Compares neighbours, swaps many times |
| Swaps | Very few — at most one per pass | Many — every out-of-order pair |
| Speed | Slow on big lists (O(n²)) | Slow on big lists (O(n²)) |
| Best for | Learning how sorting works | Learning how swapping works |
| Easy to write? | Yes — about five lines | Yes — about five lines |
Selection sort picks the winner of each round and places it once. Bubble sort bubbles the biggest number to the end, one swap at a time. Both belong to the same slow-but-simple family — computer scientists call their speed O(n²), which just means doubling the list makes the work roughly four times bigger. That is why you will not find either in a real database, but you will find them in every first algorithms class. If you want to find things fast in a sorted list, binary search is the next trick to learn — it only works because lists like this one are sorted first.
3 Mistakes Every Beginner Makes
I made all three of these, so you do not have to. First, swapping with the wrong position: beginners find the smallest number and swap it with the last number instead of the first unsorted one — remember, it always goes to the front of the unsorted part. Second, forgetting the last element: with five numbers you only need four passes, because when four are in place, the last one has nowhere else to go. Third, starting the search from the wrong place: after each pass, start scanning from the next unsorted spot, not from the very beginning, or you will "sort" the same numbers twice. Honestly, if your sort ever looks wrong, check these three things first — wrong swap spot, one pass too many, or a search that started over.
Test Your Understanding
Quiz: Sara's list is [29, 10, 14, 37, 13]. After the FIRST pass of selection sort, what does the list look like?
- [10, 29, 14, 37, 13]
- [10, 13, 14, 29, 37]
- [29, 10, 14, 37, 13]
- [13, 10, 14, 29, 37]
Answer: A. The first pass finds the smallest number (10) and swaps it into the first spot — the rest of the list is untouched until the next pass.
Conclusion
Selection sort is the simplest sorting idea in programming: find the smallest, put it first, repeat. Sara's five marks needed just four passes, and every pass locked one number into its final place. So do one thing right now: grab five random numbers, write them on paper, and sort them with this method. Feel how the sorted part grows from the left. That little moment — the smallest number sliding into place — is the whole magic of selection sort, and now you know it well enough to code it.
📅 Want a study plan for this topic?
Get a free 7-day learning plan in your calendar — one small lesson every day.


Leave a Reply