简体   繁体   English

合并两个排序数组的最坏情况下的比较次数?

[英]Number of Comparison in Worst Case of Merging Two Sorted Array?

Given two sorted arrays A, B with size n and m . 给定两个大小为nm排序数组A, B I am looking for worst number of comparison that merges these two arrays. 我正在寻找合并这两个数组的最差比较数。

1) n+m-1 1)n + m-1

2) max(n,m) 2)最大(n,m)

3)min (m,n) 3)分钟(m,n)

4) mn 4)百万

I know this is not a good question because the merge algorithm not mentioned, but i think, The normal merge sort algorithm - merge step with normally apply n + m -1 comparisons, where one list is of size n and and the other list is of size m. 我知道这不是一个好问题,因为没有提到合并算法,但是我认为,正常的合并排序算法-合并步骤通常应用n + m -1比较,其中一个列表的大小为n,另一个列表的大小为n尺寸为m Using this algorithm is the most simplest approach to combine two sorted lists. 使用此算法是合并两个排序列表的最简单方法。 any expert could help me, am I right by choosing (1)? 任何专家都可以帮助我,我选择(1)对吗?

This can easily be found out from the documentation : 可以从文档中轻松找到:

Complexity 复杂

At most std::distance(first1, last1) + std::distance(first2, last2) - 1 comparisons. 最多std :: distance(first1,last1)+ std :: distance(first2,last2)-1个比较。

So No. 1 it is. 所以是第一。 (Yes it assumes that the standard committee got the complexity right, but that is not a stretch.) (是的,它假设标准委员会的正确性是正确的,但这不是一个难题。)

Option 4 is obviously false because n + m - 1 grows slower than n*m, so we already have a better estimate. 选项4显然是错误的,因为n + m-1的增长速度比n * m慢,因此我们已经有了更好的估计。

Option 3 is false with this counterexample: 在此反例中,选项3为假:

[4], [1, 2, 6, 7]

needs at least two comparisons. 需要至少两个比较。 Option 2 counterexample: 选项2反例:

[1,6], [2,5]

would need 3 comparisons: 需要进行3个比较:

1 < 2?, 6 < 2?, 6 < 5?

Assuming m < n, at least m comparisons and at most n+m-1 comparisons (worst case). 假设m <n,则至少进行m次比较,最多进行n + m-1次比较(最坏的情况)。 So assuming all elements of the smallest list come first, the minimum number of comparisons is min (n, m). 因此,假设最小列表中的所有元素排在最前面,则比较的最小次数为min(n,m)。 Assuming that by simplest you mean best case, then answer 3 is the correct answer. 假设最简单的意思是最好的情况,那么答案3是正确的答案。 Answer 1 is correct for the worst case. 对于最坏的情况,答案1是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM