سوال ۵۶ کنکور ارشد مهندسی کامپیوتر ۱۴۰۵
میخواهیم لیست مرتب، هرکدام با عنصر را در هم ادغام کنیم و لیست مرتب تایی ایجاد کنیم. برای این کار ابتدا لیست ۱ را با لیست ۲ ادغام میکنیم. سپس لیست حاصل را با لیست ۳ و به همین روند ادامه میدهیم. زمان اجرای این الگوریتم در بدترین حالت، کدام است؟
پاسخ صحیح: گزینهٔ D —
This is the naive sequential merge of sorted lists, where a running accumulator is repeatedly merged with the next list.
Merging two sorted lists of sizes and costs . Each of the lists holds elements.
After the first lists have been merged, the accumulator holds elements. Merging it with the next list (another elements) costs
Summing over the merge steps ():
So the worst-case time is , i.e. — option 4.
Intuitively, the first list's elements get re-scanned in every one of the merge passes, so the early elements are touched times, giving overall.
Exam trick: Sequential (accumulator) merging is not . The bound is achieved only by balanced pairwise merging (a merge tree of depth ) or a min-heap -way merge. Naive left-to-right merging repeatedly re-copies the growing prefix, so it degrades to .