سوال ۶۶ کنکور ارشد علوم کامپیوتر ۱۳۹۸
در الگوریتم مرتبسازی ادغامی (Merge sort) برای مرتبسازی لیستهای زیر ۲۰ عنصری از الگوریتم مرتبسازی درجی (Insertion sort) استفاده میکنیم، در اینصورت پیچیدگی زمانی الگوریتم چه خواهد شد؟
پاسخ صحیح: گزینهٔ C —
This is the classic coarsening of merge sort (CLRS Problem 2-1): once a subarray drops to a fixed small size (here ), stop recursing and sort that block with insertion sort instead. The crucial point is that is a constant, independent of .
Cost of the insertion-sort phase. Insertion sort on a block of size costs in the worst case. There are such blocks, so the total cost of sorting all blocks is
Since is constant, .
Cost of the merging phase. After the blocks are sorted, merge sort still merges them back up the tree. Starting from blocks of size up to the full array of size , the number of merge levels is
and each level does work merging. So merging costs .
Total.
The merging term dominates, so the asymptotic complexity is unchanged from plain merge sort. The answer is option 3, .
Pitfall: The trap answers (, ) come from treating the insertion-sort blocks as if their size grew with . The cutoff size is a constant, so per block and the quadratic behavior never appears asymptotically. (Coarsening only improves the hidden constant in practice; it never changes the class.)