Data Structures and Algorithms (DSA27) · Week 9

Basic sorting (Lecture 09)

These quizzes are for self-study. Your answers are saved on this device only and are not sent anywhere.

Score: 0 / 22 · Answered: 0

  1. W9-M01 Recall

    A sorting algorithm is stable when:

  2. W9-M02 Recall

    A sort is in place when:

  3. W9-M03 Recall

    Which of these sorts is not stable, as written in the course?

  4. W9-M04 Apply

    Bubble sort, with the early exit, on [1, 2, 3, 4, 5, 6] makes how many comparisons?

  5. W9-M05 Apply

    Selection sort on [1, 2, 3, 4, 5, 6] makes how many comparisons?

  6. W9-M06 Apply

    The array after the first pass of bubble sort on A is:

  7. W9-M07 Apply

    The array after the first round of selection sort on A is:

  8. W9-M08 Apply

    Insertion sort on A. The array after the round that inserts a[3] (the value 1) is:

  9. W9-M09 Apply

    How many inversions does [3, 1, 2] have?

  10. W9-M10 Explain

    The number of shifts insertion sort makes is always equal to:

  11. W9-M11 Recall

    The best case of insertion sort is:

  12. W9-M12 Explain

    Selection sort is the best of the three basic sorts when:

  13. W9-M13 Apply

    The largest number of swaps selection sort makes on 10 values is:

  14. W9-M14 Apply

    counting_sort([4, 0, 4, 1]) makes a counts array of how many slots?

  15. W9-M15 Recall

    Counting sort on n values from the range 0 .. k - 1 takes:

  16. W9-M16 Explain

    Counting sort is a bad choice when:

  17. W9-M17 Recall

    In the worst case, every comparison sort of n values needs at least:

  18. W9-M18 Apply

    The fewest comparisons that can sort any 4 values in the worst case, by comparing, is at least:

  19. W9-M19 Explain

    The decision-tree argument for the lower bound uses the fact that:

  20. W9-M20 Explain

    A _steps generator yields its working list itself (not a copy) before each comparison. Then frames = list(generator) holds:

  21. W9-M21 Explain

    A plain sort runs its _steps generator to the end, and the generator copies the array before each of its about n2/2 comparisons. The plain sort costs:

  22. W9-M22 Apply

    Bubble sort on [6, 5, 4, 3, 2, 1] makes how many swaps?

20 marks

Written questions

5 written questions (20 marks). Write your answer first, then open the model answer and its marking points, and mark yourself honestly: each point is worth about one mark.

Written total: 0 / 20

  1. W9-E1 Explain 4 marks

    Define stable and in place. Say which of bubble, selection and insertion sort are stable, and why, from their code. Give a small input on which selection sort is not stable, and explain why stability matters when you sort by two keys.

  2. W9-E2 Apply 4 marks

    Compare bubble, selection and insertion sort: the comparisons they make at best and at worst, and the swaps or shifts. For each, describe a situation in which it is the best of the three.

  3. W9-E3 Explain 4 marks

    Define an inversion. Prove that insertion sort makes exactly one shift per inversion, and use that to give its best, worst and average case. Why do library sorts such as Timsort use insertion sort inside?

  4. W9-E4 Explain 4 marks

    Explain the _steps generator pattern of dsa/sorting.py: what it yields and when, how the plain sort reuses it, the two rules its frames must obey, and what "the price of watching" is and how it is fixed.

  5. W9-E5 Explain 4 marks

    State the lower bound for comparison sorting and give the decision-tree argument for it. Explain why counting sort does not contradict it, and when counting sort is the right choice.