The manager wants the top sellers
Your manager asks for this week's five best-selling products. The sales arrive in the order they happened.
Put them in order first, and the answer is the first five rows.
Sorted data makes reports readable, puts repeats next to each other, and lets binary search work.
By the end of this lesson
Note
I can sort data and say why it matters.
Key words
| Word | What it means | Example |
|---|---|---|
| Sort | to put items in order | |
| Ascending | smallest first, or A to Z | |
| Descending | largest first, or Z to A | reverse=True |
| Swap | to make two items change places | |
| Pass | one trip through the whole list | |
| Bubble sort | a sort that swaps neighbours in the wrong order |
The cells students fill in are left blank in this preview.
Python sorts a list for you
sorted(sales) gives you a new list in ascending order. The original list stays as it was.
sales.sort() puts the list itself in order. Nothing new is made.
Add reverse=True to either for descending order. Text sorts A to Z in the same way.
Try it: sort the week's sales
# Sales this week, in pounds, in the order they came in.
sales = [120, 45, 310, 80, 205]
print(sorted(sales)) # a new list, smallest first
print(sorted(sales, reverse=True)) # a new list, largest first
print(sales) # the original is unchanged
sales.sort() # sorts the list itself
print(sales)
names = ["Wei", "Amara", "Owen", "Bea"]
print(sorted(names)) # text sorts A to Z
# Print only the top three sales, then press Run.
A bubble sort swaps neighbours
Inside, a sort is just compares and swaps. A bubble sort is the easiest one to follow.
It compares the first two items. If they are the wrong way round, it swaps them. Then it moves one place along and does the same.
One trip through the list is a pass. It keeps making passes until a pass needs no swaps.
Worked example: sorting 5, 2, 4, 1
| Pass | List after the pass | Swaps in the pass |
|---|---|---|
| Start | 5, 2, 4, 1 | none yet |
| 1 | 2, 4, 1, 5 | 3: the 5 moves to the end |
| 2 | 2, 1, 4, 5 | 1: the 4 and the 1 |
| 3 | 1, 2, 4, 5 | 1: the 2 and the 1 |
| 4 | 1, 2, 4, 5 | 0: so the list is sorted |
The cells students fill in are left blank in this preview.
Try it: a bubble sort
numbers = [5, 2, 4, 1]
passes = 0
swapped = True
while swapped:
swapped = False
passes = passes + 1
for index in range(len(numbers) - 1):
# Compare each pair of neighbours.
if numbers[index] > numbers[index + 1]:
# Wrong way round, so swap them.
first = numbers[index]
numbers[index] = numbers[index + 1]
numbers[index + 1] = first
swapped = True
print("After pass", passes, numbers)
# Change the list to [9, 7, 3, 8, 1], then press Run.
A common mistake
Warning
Many people think numbers always sort as numbers. Numbers read from a file or a form are often text. Text sorts one character at a time, so "100" comes before "25". Turn each one into a number with int() before you sort.
See it: numbers stored as text
# Order quantities read from a spreadsheet arrive as text.
quantities = ["9", "100", "25", "3"]
print(sorted(quantities)) # sorted as text, character by character
numbers = []
for one in quantities:
numbers.append(int(one)) # turn each one into a whole number
print(sorted(numbers)) # sorted as numbers