Complete the code to perform a comparison-based sort using Python's built-in method.
numbers = [5, 3, 8, 1] numbers.[1]()
The sort() method sorts the list in place using comparison-based sorting.
Complete the code to perform a non-comparison based sort using counting sort logic for small integers.
def counting_sort(arr): count = [0] * (max(arr) + 1) for num in arr: count[num] += 1 sorted_arr = [] for i in range(len(count)): sorted_arr.extend([i] * [1]) return sorted_arr
We extend the sorted array by repeating the number i as many times as it appears, which is stored in count[i].
Fix the error in the code that attempts to sort a list using a comparison-based method but incorrectly uses a function.
def sort_list(lst): return sorted(lst, [1]=True)
The correct keyword argument is reverse which takes a boolean to sort descending if True.
Fill both blanks to create a dictionary comprehension that maps numbers to their frequency, a step in non-comparison sorting.
freq = {num: [1] for num in arr if arr.count(num) [2] 0}The dictionary maps each number to how many times it appears using arr.count(num). The condition checks if the count is greater than zero.
Fill all three blanks to create a list comprehension that extracts keys from a frequency dictionary where frequency is at least 2.
result = [[1] for [2], [3] in freq.items() if [3] >= 2]
The comprehension iterates over freq.items() unpacking keys and values as k and v. It collects keys k where value v is at least 2.
