Level 1 Sort segments in parallel, then merge
A service wants to speed up sorting by splitting the work across threads. Write
parallel_sort(nums: list[int], k: int, sort_segment, merge_pair=None) -> list[int]
that returns all of nums in ascending order, following these rules (the tests check each one):
- Split
numsintom = min(k, len(nums))contiguous segments, as even as possible: sizes differ by at most one, and the longer ones come first. E.g. 10 items,k = 4→ sizes 3, 3, 2, 2. - One thread per segment. Call
sort_segment(segment)exactly once for each segment, wheresegmentis a new list holding only that segment's items (a thread may not touch anything outside its own segment). Each call must run in its own thread, not the calling thread, and allmcalls must be running at the same time: the test'ssort_segmentwaits until allmthreads have arrived before any of them continues. sort_segmentreturns the sorted segment (use the returned list; don't assume it sorted in place).- Merge the
msorted segments into the final sorted list (in the calling thread for this level; ignoremerge_pair). Aim for O(n log m) for the merge; the tests merge 300,000 items from 64 segments and time it. - If any
sort_segmentcall raises,parallel_sortraises that exception (after the threads have finished). - Empty
numsreturns[]without calling anything.k >= 1.
parallel_sort([5, 1, 4, 2, 3, 0], 4, sorted) # [0, 1, 2, 3, 4, 5]
# segments: [5, 1] [4, 2] [3] [0] (sizes 2, 2, 1, 1)
Show hint
threading.Thread per segment (or a ThreadPoolExecutor(max_workers=m), which does start m threads here), collect results by index, join all, then heapq.merge(*runs). Store exceptions and re-raise after joining.