Level 5

Sort the List

Level 5 · Chapter 12

What this lesson follows

CSTA K–12 Computer Science Standards (2017)

Algorithms & Programming · Grades 6–8

Plan a hard problem with a flowchart or pseudocode.

Code 2-AP-10Read the official text

Also practised: 2-AP-12 · 2-AP-17

Common Core State Standards for Mathematics

The Number System · Grade 6

Use negative numbers on the number line.

Code 6.NS.C.7Read the official text

The short lines are our own words. We matched this lesson to these standards ourselves; the standards' owners have not reviewed or endorsed this course.

Common Core State Standards © 2010 National Governors Association Center for Best Practices and Council of Chief State School Officers. All rights reserved.

Today you will implement bubble sort on a Scratch list — applying loops, comparisons, and list operations.

1👋 Warm-Up

For teachers · goals · guide
Lesson Outcomes · for teachersCSTA 2
✦ CSTA 2-AP-10 · CSTA 2-AP-12 · CSTA 2-AP-17 · ISTE 1.5.c · Standards
🎯 I canimplement bubble sort on a Scratch list — applying loops, comparisons, and list operations
🧩 New blocks1 block
📝 New words8 words
⏱ Lesson time~40 min
Teacher Guide

📋 Before class

⚠️ Common misconceptions

  • Students often try to swap by writing item(i) = item(j) directly. This overwrites item(i) before saving it. The temp variable must capture item(i) FIRST before any replacement happens.
  • Students may think one pass through the list is enough to sort it. Bubble sort requires up to (n-1) full passes to guarantee sorting. After each pass, the largest unsorted item bubbles to the correct end.

🎯 Differentiation

🐢 Struggling: Focus on the swap alone first — practice swapping just items 1 and 2 using temp. Once the swap works, add the nested loop.

🚀 Advanced: Challenge them to add a sorted flag: if no swaps occurred during a full pass, the list is already sorted and you can stop early. This is the optimized bubble sort.

💡 During the lesson

  • Warm-UpUse physical index cards with numbers if available. Have three students stand holding cards (e.g., 7, 2, 5) and physically swap positions as the class walks through one pass. This tactile experience makes the nested loop structure tangible before any code is written.
  • Try ItThe wrong option (item 1) in the second experiment is intentional. Walk through it step by step: after line 1, item(1) now contains item(2)'s value. Line 2 then copies that value back — both slots now hold the original item(2), and item(1)'s original value is gone forever. This is a classic novice bug.
    In the third experiment, 1 pass moves only the largest item to the end. More passes add the outer loop — students see the list converge after each pass. Ask what would happen without the if: every pair would swap, which is shuffling, not sorting. Ask students to trace the third experiment on paper after each pass. A list of 4 items needs 3 passes — one less than its length.
  • PlanThe nested loop structure is the most complex flowchart students have seen. Spend time on the two distinct loops: the outer loop counts passes, the inner loop scans neighbors within each pass. The IF inside the inner loop only fires when a swap is needed. Ask: "How many total times does the inner loop body run for the plan's 4-item list?" (3 × 3 = 9 comparisons every time — at most 6 of them swap.) Do the first box together, then pick and watch both stages.
  • BuildThe say [Swapped!] for (0.3) seconds block in step 4 is intentional (you watch it in step 6) — students can watch each swap happen, reinforcing the algorithm visually. Remove it in step 7 to show efficiency. For step 9, changing from 5 to 8 items is a good test of whether the algorithm is correct — it should scale because the repeat counts use (length of [numbers ▾]) dynamically, not a hardcoded 5.