这些任务有多大的复杂性?

时间:2014-05-13 19:25:21

标签: big-o

作为期末考试修订的一部分,我有以下问题:

For each of the following problems give the worst-case running time in
Big-O notation.

(i) Adding n numbers
(ii) Finding the minimum of n numbers in an unordered array 
(iii) Finding an item in a binary heap 
(iv) Sorting an unordered list of items using merge sort
(v) Finding the median (The value of a numerical set that equally divides
the number of values that are larger and smaller) of an array of sorted
items 

我目前的想法是否正确?

(i) This would be O(n) because you are adding n numbers.
(ii) This again would be O(n). You have to check every element in this list.
(iii) Not 100% sure here, but i assume it would be worst case O(n log n) as most things are with binary heaps.
(iv) This would be O(n log n)
(v) Again i am not sure on this, maybe O(log n) since the array is sorted so you only need to search half the values, essentially a binary chop.

如果我的任何答案都不正确,有人会指出我正确的方向。

谢谢, 克里斯。

2 个答案:

答案 0 :(得分:1)

  

(v)求中位数(等分的数值集的值)   排序数组的值越大越小   项目

     

(v)我不确定这个,也许是O(log n),因为数组已经排序,所以你只需要搜索一半的值,基本上是二进制的。

这个是O(1)。您感兴趣的是排序数组中间的项目(对于N奇数),或两个“最接近”到中间的平均值(对于N偶数)。

由于数据是有序的,您可以简单地检查结果所需的一个或两个元素。

答案 1 :(得分:1)

  

(iii)在二进制堆中查找项目

     

(iii)这里不是100%肯定,但我认为最糟糕的情况是O(n log   n)因为大多数事情都是二进制堆。

这实际上是O(N),因为它可以通过遍历构建堆的二叉树来解决。