Hi guys,
I don’t understand your remark at the end of 11.1 solution:
Alternatively, we could recursively merge the k files, two at a time using the merge step from merge sort. The time complexity is the same, i.e. O(n log k). The space complexity of any reasonable implementation of merge sort is O(n), which is considerably worse than the heap based approach when k << n.
- if we merge 2 files at a time, then we just need space for 2 records from each file at any given moment. You say about O(n) space of merge sort, but we are not doing sort, we are doing just merge here.
- the time complexity of repeating 2-way merges will be actually worse than heap-based solution, because we compare the same elements multiple times: during 1st merge we compare elements of A and B, during 2nd merge we compare (A+B) and C, during 3rd merge - (A+B+C)+D. So elements of A will be compared (k-1) times, of B (k-2) time and so on. This leads to O(nk) complexity.