CurriculumAlgorithmic Thinking in Python, for KidsSorting, and the edge cases every pattern needs

Selection Sort in Python, and the Edge Cases That Break Loops

Watch a real sorting algorithm work, then find the gaps every pattern from this topic has been hiding.

Lesson 3 of 4PROAges 10+
See what PRO unlocks →

Sorting, and the edge cases every pattern needs

You already know .sort() sorts a list — one line, done. This is about HOW a computer could sort one, if it had to figure it out step by step.

Selection sort: find the SMALLEST remaining item, swap it to the front of what's left unsorted, repeat.

values = [50, 20, 80, 10]
# pass 0: smallest overall is 10, swap into position 0
# [10, 20, 80, 50]
# pass 1: smallest of [20, 80, 50] is 20, already in position 1
# [10, 20, 80, 50]
# pass 2: smallest of [80, 50] is 50, swap into position 2
# [10, 20, 50, 80]
# pass 3: only 80 left — nothing to compare, stays put
# [10, 20, 50, 80]

Each pass narrows the unsorted part by one item — the front keeps growing correctly sorted, one item at a time.

This is a TEACHING tool, not something you'd write in real code — .sort() is always the right practical choice. The value is seeing that sorting isn't magic; it's a pattern like any other.

Now, edge cases. Every pattern from this topic has an assumption baked in that can break.

Max-tracking assumes there's at least ONE item — scores[0] crashes outright if scores is empty.

scores = []
highest = scores[0]

Output

IndexError: list index out of range

The counter pattern has a quieter trap: if every item is EXACTLY equal to what you're comparing against, a strict > test can reject all of them — 5 > 5 is False, even though they're not smaller.

scores = [5, 5, 5]
count = 0
for s in scores:
    if s > 5:
        count = count + 1
print(count)

Output

0

Neither of these is a bug in the PATTERN — they're the pattern working exactly as written, on an input its author didn't think to check for.

Check your understanding

scores = []. highest = scores[0] runs before any loop. What happens?

scores = []
highest = scores[0]
  • IndexError — position 0 doesn't exist in an empty list
  • highest becomes 0, since that's a sensible default for an empty list

Why: IndexError. scores[0] asks for the FIRST item, and an empty list has no items at any position — not position 0, not any position. Any pattern that starts by grabbing scores[0] needs an empty-list check before it, or it crashes the instant the list happens to be empty.

scores = [5, 5, 5], and the counter pattern counts values where s > 5. What's the final count?

count = 0
for s in scores:
    if s > 5:
        count = count + 1
  • 0 — 5 is never STRICTLY greater than 5
  • 3 — every item is 5, so every item should count

Why: 0. s > 5 is strictly greater-than, and 5 is never strictly greater than 5 — so every item fails the test, even though intuitively they all 'match' 5. Whether this is the right test depends entirely on what the problem actually asks for — > and >= genuinely disagree on this exact input.

What you'll practice

values = [50, 20, 80, 10]. Selection sort repeatedly finds the SMALLEST remaining item and swaps it into place. Run the code as it is and watch values change after each pass — the final result should be [10, 20, 50, 80].

Unlock this lesson with PRO →

More from this topic

Sorting, and the edge cases every pattern needs is one lesson inside Algorithmic Thinking in Python, for Kids — see the full lesson order and what the whole topic covers.

View the full topic →