سوال ۵۷ کنکور ارشد مهندسی کامپیوتر ۱۴۰۴
دنباله یک دنباله دوآهنگی (Bitonic) است، بدین معنی که هرگاه عناصر دنباله را بهترتیب دور یک دایره بنشانیم، یک عنصر یافت میشود که دنباله عناصر بعدی ابتدا صعودی و سپس نزولی هستند. بهترین زمان برای مرتبسازی یک دنباله دوآهنگی، کدام مورد است؟
پاسخ صحیح: گزینهٔ D —
A bitonic sequence is one that, read around a circle, consists of exactly one ascending run followed by one descending run. Equivalently, it is the concatenation of a non-decreasing run and a non-increasing run. The key observation is that such a sequence is essentially two already-sorted lists glued together, so we never need a full comparison sort.
Algorithm (linear time):
- Scan the array once to find the peak (the single point where the sequence stops ascending and starts descending). This is .
- The peak splits into an ascending prefix and a descending suffix. Reverse the descending suffix (or read it back-to-front) so you have two sorted runs.
- Merge the two sorted runs with a single linear pass, exactly like the merge step of merge sort. This is .
Total cost:
Why the other options are not the best time:
- is what a general comparison sort (merge/heap/quick) costs, but it ignores the bitonic structure.
- and are even slower and unnecessary.
Because a comparison-based sort needs just to read the input, is also optimal here — the bitonic property removes the factor.
Exam trick: A bitonic (or any "sorted then reversed-sorted") sequence is the merge of two monotone runs, so it sorts in linear time — no need to pay the general comparison-sort bound.