Data Structures and Algorithms (DSA27) · Week 10

Advanced sorting (Lecture 10)

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. W10-M01 Recall

    Which of these is not one of the three steps of divide and conquer?

  2. W10-M02 Apply

    The recurrence for the number of steps of merge sort on n elements is:

  3. W10-M03 Apply

    _merge merges two sorted runs of 4 elements each. The largest number of comparisons it can make is:

  4. W10-M04 Explain

    Merge sort is stable because:

  5. W10-M05 Recall

    The extra space merge sort needs, beyond the input, is:

  6. W10-M06 Apply

    How many calls of _merge does merge_sort make on 8 elements?

  7. W10-M07 Apply

    merge_sort(list(range(1024))) — already sorted — makes how many comparisons?

  8. W10-M08 Recall

    During Lomuto's partition, with j the next index to look at, the invariant is:

  9. W10-M09 Apply

    _partition(a, 0, 7) on a = [6, 3, 9, 1, 8, 2, 7, 4] returns:

  10. W10-M10 Apply

    How many comparisons with the pivot does one Lomuto partition of a range of 8 elements make?

  11. W10-M11 Explain

    quick_sort(list(range(n)), pivot="first") makes:

  12. W10-M12 Explain

    On an already sorted range, pivot="median3" avoids the worst case because:

  13. W10-M13 Explain

    Quicksort recurses into the smaller part and loops on the larger. This bounds:

  14. W10-M14 Recall

    Quicksort is not stable because:

  15. W10-M15 Recall

    In the array layout of a heap, the children of index i are at:

  16. W10-M16 Apply

    Build-heap on [4, 10, 3, 5, 1, 8, 2, 7] gives:

  17. W10-M17 Explain

    Build-heap is O(n), not O(n log n), because:

  18. W10-M18 Apply

    For n = 10, heap sort's build loop sifts down the indices:

  19. W10-M19 Recall

    Which sort is O(n log n) in the worst case and needs only O(1) extra space?

  20. W10-M20 Explain

    The lower bound of about n log2 n comparisons applies to:

  21. W10-M21 Recall

    Python's sorted() and list.sort() use:

  22. W10-M22 Apply

    sorted(list(range(1000))) — already sorted — makes how many comparisons?

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. W10-E1 Explain 5 marks

    Describe merge sort as divide and conquer. Write its recurrence and solve it with a recursion tree. Give its extra space, and explain what makes it stable.

  2. W10-E2 Explain 4 marks

    Describe Lomuto's partition: what it does, its invariant, and why the pivot is in its final place afterwards. Why is there nothing to "combine" in quicksort?

  3. W10-E3 Explain 4 marks

    Compare the pivot strategies "first", "random" and "median3": the cost of choosing, and the inputs on which each is at its worst. Why is sorted input the case that matters?

  4. W10-E4 Explain 4 marks

    Explain how heap sort uses an array as a tree. Describe build-heap and the sort phase, and argue that build-heap is O(n). Why is heap sort often slower in practice than quicksort, though its worst case is better?

  5. W10-E5 Explain 3 marks

    Define a stable sort. Give a practical reason to want one, say which of the six sorts in dsa/sorting.py are stable, and explain why sorting (key, tag) tuples does not test stability.