← Back to course
Code Crew G5 / Week 08 / Tuesday
2/6
Week 08 Β· Sorting by hand

Tuesday

Count comparisons and swaps
// Selection sort and sorted()
⏱ about 15 min

Tuesday: Count comparisons and swaps

Rocket sorts cards for Nova and keeps a tally. Every time he checks two cards, he makes a mark.

"Ten marks for five cards," Rocket says. "Is that always the number?"

Raven sorts five cards that are already in order. She makes marks too. "What do you notice about my tally?"

"It is ten again! Even though nothing moved," Rocket says.

Nova blinks. "Here is a hint. Count how many cards each pass looks at."

Comparisons

A comparison checks two cards to see which is smaller. To find the smallest of 5 cards, you need 4 comparisons.

Pass 1 looks at 5 cards, so 4 comparisons. Pass 2 looks at 4 cards, so 3. Then 2, then 1.

PassCards looked atComparisons
154
243
332
421
Total10

Selection sort always makes the same number of comparisons for the same number of cards. It checks every unsorted card each pass, even when the list is already sorted.

How many comparisons does selection sort make for 6 cards?
WHY THIS EXERCISEEach pass compares one fewer card than the pass before.

Swaps

A real swap is when two different cards trade places. If the smallest card is already in its spot, nothing moves.

Starting cardsReal swaps
7, 3, 9, 1, 52
1, 2, 3, 4, 50
5, 4, 3, 2, 12
COUNT THE SWAPS.
  • Read the question.
  • Tap your answer.
How many real swaps to sort 8, 6, 7, 5?
How many comparisons to sort 8, 6, 7, 5?
Statement about selection sortTrue or false?
A list that is already sorted needs 0 comparisons.?
A list that is already sorted needs 0 real swaps.?
Sorting 10 cards takes more comparisons than sorting 5.?
WHY THIS EXERCISECounting steps shows how much work an algorithm does, even when it looks like nothing happened.

Practice lab: let Python count

def count_comparisons(items):
    n = len(items)
    count = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            count = count + 1
    return count

print(count_comparisons([7, 3, 9, 1, 5]))
print(count_comparisons([1, 2, 3, 4, 5]))
print(count_comparisons([4, 2, 8, 6, 1, 3]))
What does the first print line show?
What does the second print line show?
What does the third print line show?
How many comparisons for 4 cards?
COUNT CHECK
  • Read the question.
  • Tap your answer.
How many comparisons happen in pass 1 for 8 cards?
How many real swaps to sort 3, 1, 2?
How many real swaps to sort 1, 2, 3?
StatementTrue or false?
count_comparisons ignores the values in the list.?
A reversed list needs more comparisons than a sorted one.?
7 cards need 21 comparisons.?
The number of swaps can change with the starting order.?
WHY THIS EXERCISEComparisons stay fixed, but swaps change with the starting order.
How many comparisons does selection sort make for 10 cards?
WHY THIS EXERCISEEach pass compares one fewer card than the pass before.

Nice counting! Tomorrow we turn the card method into Python code.

← Monday