简体   繁体   English

为算法的基本运算计数建立递归关系并求解

[英]Set up a recurrence relation for the algorithm’s basic operation count and solve it

Consider the following algorithm 考虑以下算法

ALGORITHM Find(A[0..n‐1])
    if n ==1 return A[0]
    else temp = Find(A[0..n‐2])
    if temp ≤ A[n‐1] return temp
    else return A[n‐1]

a. What does this algorithm compute?
b. Set up a recurrence relation for the algorithm’s basic operation count and solve it.

Does this algo return A[0],A[0..3],A[0..5],A[0.7],A[0..8], perhaps for n=9? 这个算法是否会在n = 9时返回A [0],A [0..3],A [0..5],A [0.7],A [0..8]? Am I on the right track? 我在正确的轨道上吗?

Appreciate if someone could give me along! 感谢有人可以陪伴我! Thanks! 谢谢!

This algorithm will recursively calculate the minimum of the given array or list of elements. 该算法将递归计算给定数组或元素列表的最小值

For every value of n . 对于n每个值。 You calculate he minimum of all values preceding n (ie. <= n - 1). 您计算n之前所有值的最小值(即<= n-1)。 If the value returned is less than the value[n] , you return that value else you return value[n] . 如果返回的值小于value[n] ,则返回该值,否则返回value[n]

The base case is trivial when you have only one element. 当您只有一个元素时,基本情况是微不足道的。 You return that value as the minimum. 您返回该值作为最小值。

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

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