Grade 9 Computer Science — Putting a list in order
Take a list along once, swapping neighbors 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.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.
- 1
- 6
- 9
- 3
2.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?
3.This program does one pass along its list, using a spare variable to hold a value while the swap happens. What does it print?
values = [7, 4, 9, 2] for i in range(0, 3): if values[i] > values[i + 1]: left = values[i] values[i] = values[i + 1] values[i + 1] = left print(values[3])4.A list has 7 values in it. At most how many passes could putting it in order ever need?
- a) 5
- b) 6
- c) 7
- d) 21
5.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?
6.Each of these lists is taken along once, swapping neighbors 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
- 3
- 2
- 0
- 1
7.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)8.After one whole pass along a list, one value is certain to be in its final place. Which one?
- a) Nothing can be said for certain after only one pass
- b) The smallest value in the list
- c) The largest value in the list
- d) Whichever value started at position 0
Answer key — Grade 9 Computer Science — Putting a list in order
- 1. 1. 3 2. 6 3. 1 4. 9
- 2. 1
- 3. 9
- 4. b) 6
- 5. 25
- 6. [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
- 7. 2
- 8. c) The largest value in the list