Year 10 Computing — 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.A list has 5 values in it. Mark how many comparisons a single pass along it makes.
Mark the line with an X.
2.A list has 9 values in it. How many comparisons does a single pass along it make?
3.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)4.Each of these lists is taken along once, swapping neighbours that are the wrong way round. Match each list to the number of swaps that single pass makes.
- [2, 1, 4, 3]
- [1, 2, 3]
- [5, 4, 3, 2]
- [1, 2, 3, 4, 5, 9, 6]
- [8, 7, 6, 5, 4]
- 4
- 0
- 1
- 2
- 3
5.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
6.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?
7.The list 4, 3, 2, 1 is exactly the wrong way round. How many swaps happen during a single pass along it?
8.After one whole pass along a list, one value is certain to be in its final place. Which one?
- a) The largest value in the list
- b) The smallest value in the list
- c) Nothing can be said for certain after only one pass
- d) Whichever value started at position 0
Answer key — Year 10 Computing — Putting a list in order
- 1. 4
- 2. 8
- 3. 2
- 4. [2, 1, 4, 3] → 2; [1, 2, 3] → 0; [5, 4, 3, 2] → 3; [1, 2, 3, 4, 5, 9, 6] → 1; [8, 7, 6, 5, 4] → 4
- 5. b) 6
- 6. 25
- 7. 3
- 8. a) The largest value in the list