Class 9 Computer Science — Putting a list in order
Take a list along once, swapping neighbours that are the wrong way round, and count the comparisons, the swaps and the passes that putting a whole list in order takes.
Name: ________________________
1.After one whole pass along a list, one value is certain to be in its final place. Which one?
- a) Whichever value started at position 0
- b) The smallest value in the list
- c) The largest value in the list
- d) Nothing can be said for certain after only one pass
2.A list has 9 values in it. How many comparisons does a single pass along it make?
3.A list has 7 values in it. At most how many passes could putting it in order ever need?
- a) 21
- b) 6
- c) 5
- d) 7
4.This program counts its own swaps as it makes them. What does it print?
values = [3, 1, 4, 2] swaps = 0 for i in range(0, 3): if values[i] > values[i + 1]: left = values[i] values[i] = values[i + 1] values[i + 1] = left swaps = swaps + 1 print(swaps)5.A list has 5 values in it. Mark how many comparisons a single pass along it makes.
Mark the line with an X.
6.The list 6, 3, 9, 1 is taken along once in the same way. Put the values in the order they sit in once that pass is finished, the front of the list first.
- 3
- 6
- 1
- 9
7.The list 9, 1, 8, 7 is taken along once in the same way. What value is sitting at position 0 when the pass is finished?
8.A list of 6 values is put in order by making 5 passes along it, and every one of those passes makes 5 comparisons. How many comparisons is that altogether?
Answer key — Class 9 Computer Science — Putting a list in order
- 1. c) The largest value in the list
- 2. 8
- 3. b) 6
- 4. 2
- 5. 4
- 6. 1. 3 2. 6 3. 1 4. 9
- 7. 1
- 8. 25