سوال ۶۱ کنکور ارشد علوم کامپیوتر ۱۴۰۳
فرض کنید یک الگوریتم با زمان اجرای برای تعیین میانه اعضای یک آرایه عضوی موجود است. به کمک این الگوریتم بهترین زمان اجرای الگوریتم مرتبسازی سریع، چقدر است؟
پاسخ صحیح: گزینهٔ D —
Approach
In quicksort, the running time depends entirely on how balanced the partition is. If we can pick the median as the pivot, every partition splits the array into two halves of size , producing the most balanced (best possible) recursion tree.
Key insight
At each recursive call on a subarray of size we do two things:
- Find the median in time to use as pivot.
- Partition around that pivot in time .
So the recurrence is:
Steps
The recursion tree has depth (the array halves each level). At each level the total work across all nodes is bounded by the partitioning cost plus the median-finding cost. Since is at least linear (finding a median requires reading all elements, so ), at a level with subarrays of size the summed cost is dominated by terms whose total is per level (the partition work sums to and the median work, being superadditive-bounded, sums to ).
Multiplying the per-level cost by the number of levels:
This matches option 4.
Sanity check
If median-finding were free or linear, , then the bound becomes — exactly the optimal comparison-sort bound, which is consistent. If instead dominates, e.g. , we get , again matching the recurrence.
Pitfall: Do not multiply and together (options 1 and 3). The median is found once per recursive call, not once per element. The number of recursive calls is what introduces the factor, and the costs (partition) and (median) are added, not multiplied, at each level.