← Back to course
Python 11-12 / Week 09 / Monday
1/6
Week 09 Β· Sorting

Monday

Sort by hand
// Selection and insertion sort; count the work
⏱ about 30 min

Monday: Sort by Hand

Binary search needs sorted data, and the catalog is not sorted. Comet writes five sample weights on index cards and drops them on the bench: 42, 18, 65, 7, 30.

"Sorting is easy," she says, and shuffles them into order in two seconds. "Done. Next problem."

Wren smiles and holds up his clipboard. "What does the evidence say? You did it, but you cannot say how. A program needs steps it can repeat."

Nova projects the five cards in a row, with an empty box at the far left. "How can I help?" she asks. "Find the smallest card left. Move it to the next empty place. Repeat."

Comet slides the cards back into a row. "Lead programmer, talk me through it, one card at a time."

Selection sort

Selection sort repeatedly picks the smallest item from the unsorted part of the list and swaps it into its final place.

Take the cards 42, 18, 65, 7 and 30. The smallest is 7, so 7 swaps with 42, the card in the first place. Now the first place is final. Then repeat on the four cards after it.

Here is the first step in code. Read it and predict before you run it.

# smallest.py
# Made-up data from the FieldSim story: five sample weights.
nums = [42, 18, 65, 7, 30]
start = 0
smallest = start
for j in range(start + 1, len(nums)):
    if nums[j] < nums[smallest]:
        smallest = j
print("smallest at", smallest, "value", nums[smallest])
nums[start], nums[smallest] = nums[smallest], nums[start]
print(nums)
PREDICT SMALLEST.PY
  • Read the question.
  • Tap your answer.
Which index holds the smallest number?
What does the list look like after the swap?
smallest at 3 value 7
[7, 18, 65, 42, 30]
PassSmallest leftSwapCards after the pass
177 and 427, 18, 65, 42, 30
21818 with itself7, 18, 65, 42, 30
33030 and 657, 18, 30, 42, 65
44242 with itself7, 18, 30, 42, 65
PUT THE STEPS OF ONE SELECTION SORT PASS IN ORDER
  • ?Remember where the smallest card is
  • ?Mark that place as final
  • ?Swap the smallest card into the first place that is not final
  • ?Look at every card from there to the end
  • ?Start at the first place that is not final yet
WHY THIS EXERCISEEach pass searches the unsorted part for the smallest card, then makes one swap that puts it in its final place.

A swap that loses a card

Comet first wrote the swap as two separate lines. Predict what goes wrong.

# bad_swap.py
nums = [42, 18, 65, 7, 30]
nums[0] = nums[3]
nums[3] = nums[0]
print(nums)
PREDICT BAD_SWAP.PY
  • Read the question.
  • Tap your answer.
What does it print?
What kind of error is this?
[7, 18, 65, 7, 30]

Python can also swap two items in one line, as smallest.py does: nums[0], nums[3] = nums[3], nums[0]. Try it in step 3 below and see that nothing is lost.

At your computer
1. Type smallest.py in your fieldsim folder, save it and run it.
2. Change one thing: set start = 1 and run it. Which index is the smallest now, looking only from index 1 on?
3. Type bad_swap.py and run it. Then fix it with the one-line swap and run again.
4. Break it on purpose: in smallest.py, change len(nums) to len(nums) + 1. Run it and read the last line of the traceback calmly. Then change it back.

This is the run for step 2, with start = 1.

smallest at 3 value 7
[42, 7, 65, 18, 30]
READ THE START = 1 RUN
  • Read the question.
  • Tap your answer.
Why is 42 still in the first place?
StatementTrue or false?
After each selection sort pass, one more item is in its final place.?
Selection sort swaps every pair of neighbors that is out of order.?
nums[0] = nums[3] then nums[3] = nums[0] swaps two items correctly.?
Five cards need at most four passes.?
WHY THIS EXERCISESelection sort makes one swap per pass, putting the smallest card left into its final place. The last card is then already in place.

You turned "just sort it" into steps a program can repeat. Tomorrow you write selection sort as a function.