سوال ۵۷ کنکور ارشد مهندسی کامپیوتر ۱۴۰۳
آرایه به طول را مرتب میکنیم. هرگاه برای هر که داشته باشیم ، یعنی آرایه به لیست مرتب که هر کدام تقریباً عنصر دارند افراز میشود. فرض کنید یک آرایه مرتب به طول باشد. سریعترین الگوریتم برای تبدیل این آرایه به یک آرایه مرتب، از چه مرتبه زمانی است؟
پاسخ صحیح: گزینهٔ D —
Decompose the -sorted array into sorted chains.
The condition links every element to the one positions before and after it. Group the indices by residue :
Within one chain, consecutive elements are exactly the pairs and , and the hypothesis gives . Hence each of the chains is already individually sorted, and each has about elements.
Reduce to merging sorted lists.
Producing a fully (-)sorted array is therefore exactly the problem of merging sorted lists whose total length is . Using a min-heap of size (always holding the current head of each chain):
- each of the output elements is extracted with one and one ,
- every heap operation on items costs .
Why the other options fail.
| Option | Verdict |
|---|---|
| Impossible: a general comparison sort of these chains has lower bound . | |
| This is naive repeated linear merging; not the fastest. | |
| Sublinear in for fixed — cannot even touch all elements. | |
| Tight bound via heap-based -way merge. ✔ |
The matching lower bound (each comparison sort decision tree must distinguish all interleavings of sorted lists) makes optimal, so option 4 is the fastest.
Exam trick: A "-sorted" array is just presorted subsequences in disguise. Recognize it as a -way merge and the answer is always , the same cost as merging sorted lists of total size with a min-heap.