Embercode
✕
SortingQuick quiz

Partial sorting

6 questions. Covers Partial sorting in Python.

Questions 1 to 5 of 6

  1. 1. A bounded heap keeps the k smallest values seen so far. Which heap does it use, and why?
  2. 2. Why does smallest_by_selection (the lesson version) call min(k, len(copy)) before its loop?
  3. 3. What does this program print? ``python def smallest_by_selection(a, k): copy = list(a) limit = min(k, len(copy)) for i in range(limit): best = i for j in range(i + 1, len(copy)): if copy[j] < copy[best]: best = j copy[i], copy[best] = copy[best], copy[i] return copy[:limit] print(smallest_by_selection([10, 2, 8, 2, 7], 3)) ``
  4. 4. What does this program print? ``python import heapq wrong = [] for x in [3, 8, 5, 1, 7]: heapq.heappush(wrong, x) if len(wrong) > 2: heapq.heappop(wrong) print(sorted(wrong)) ``
  5. 5. A bounded heap handles n values and keeps at most k items. Roughly how many steps does it take in total?

5 question(s) left on this page.