Skip to the workspace
Pebellslearn by building
Recursion, Searching & Sorting  ›  Lesson

Elementary Sorts & the Play Chart

Elementary Sorts 25 minBubble, selection and insertion sort — what they cost and what they do to ties

dsa / Elementary Sorts & the Play Chart

Explore each scene at your own pace.

01 · Rank the Play Chart with bubble sort

Rank the Play Chart with bubble sort

Elementary Sorts & the Play Chart

Definition
Bubble, selection and insertion sort are simple sorting algorithms that compare and move items one at a time, each costing O(n²) in the worst case, and differing in stability (whether ties keep their order) and speed on nearly sorted data.
In plain words
They are three ways of tidying a hand of cards: swap neighbours that are out of order, repeatedly pick out the smallest, or slide each new card into place. All are slow on big piles but easy to follow.

In short: three simple sorts all compare neighbours or scan for the smallest, and all cost O(n²) in the worst case — but they differ in whether ties keep their order (stability) and how they behave on data that's almost sorted already. Tunebox's Play Chart ranks tracks by play count; in production that's one call to sorted(), but which sort you'd hand-roll decides what happens to ties and to a chart that just had one swap.

Three lanes labelled bubble / selection / insertion, each showing the same five numbers mid-sort: bubbles rising to the right, a hand reaching into the rest for the smallest, and a card sliding into a sorted hand

Rank the Play Chart with bubble sort

Walk the list comparing each pair of neighbours; swap any pair that's out of order. Each pass carries the largest remaining value to the end, like a bubble rising — and a pass with no swaps proves the list is already sorted, so you can stop early:

python
watch it move · array

Each pass carries the largest unplaced value to the end, and a pass with no swaps proves the list is sorted.

Pass 1 starts at the front, where 5 is bigger than 1, so this pair is out of order. 5 cells. j at index 0. Comparing 5 and 1.

1 / 10

The mistake: thinking one pass finishes more than it does. One pass only guarantees the largest value reaches the end — everything else just shuffles one place:

What does this print?
nums = [5, 1, 4, 2]
for j in range(len(nums) - 1):
    if nums[j] > nums[j + 1]:
        nums[j], nums[j + 1] = nums[j + 1], nums[j]
print(nums)
Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — search & charts
This piece — rank_tracks(): Orders tracks by play count, the simple way.
Scenario The Play Chart lists every track in your library by play count. Two tracks on 12 plays should stay in the order you added them, so the ranking has to be a stable sort.
rank_tracks.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
rank_tracks(tracks) → listTitles ranked by plays, most first, with ties kept in their original order.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.