【问题标题】:Time complexity max min [closed]时间复杂度最大最小 [关闭]
【发布时间】:2014-10-19 16:08:20
【问题描述】:

简单的线性搜索找到最大最小值算法

maxmin(a,n,max,min){
max=min=a[1];
for i=2 to n do{
    if a[i]>max then max:=a[i];
    else if a[i]<min then min:=a[i];
}
}

1. 考虑到第一个 if 条件对于 n/2 个元素失败,上述算法的平均案例复杂度 2.如果第一个ccondition失败1/2次plz xplain,则上述算法的平均案例复杂度

【问题讨论】:

  • 编程语言是什么?它是从0开始,从1开始? anminmax的内容是什么?
  • 家庭作业问题...?
  • 它的算法实际上我们必须找到比较的数量
  • 请求家庭作业帮助的问题必须包括您迄今为止为解决问题所做的工作的总结,以及您在解决问题时遇到的困难的描述。

标签: performance algorithm time divide-and-conquer


【解决方案1】:

这两种情况的平均情况复杂度为 O(n)。如果 k 是第一个 if 失败的次数,那么比较的次数是 2*n - 2 - k。

maxmin(a,n,max,min){
   max=min=a[1];
   for i=2 to n do{ // goes through the loop n-1 times
      if a[i]>max then max:=a[i]; // out of n-1 times succeeds k times and fails n-1-k times
      else if a[i]<min then min:=a[i]; // runs this n-1-k times
   }
}

n-1 + n-1-k -> 2*n - 2 - k

【讨论】:

  • 两种情况下的比较次数是?
  • 你不应该在没有任何能力的情况下回答家庭作业问题 kriss singh...
  • 非常感谢,但请您解释一下您是如何得到它的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
相关资源
最近更新 更多