早教吧作业答案频道 -->英语-->
学过分而治之算法的人进来看看Considerthefollowingproblem.GivenanarrayAconsistingofndistinctintegersA[0],…A[n-1].Itisknownthatthereisapositionpbetween1andn,suchthatA[0]A[n-1].1.Designadivide-and-conqueralg
题目详情
学过分而治之算法的人进来看看
Consider the following problem.
Given an array A consisting of n distinct integers A[0],… A[n-1].It is known that there is a position p between 1 and n,such that A[0] A[n-1] .
1.Design adivide-and-conquer algorithmto find the position p with running time of O(log n) in the worst case
2.Justify the running time of your algorithm by
i.Finding the recurrence relation for its time complexity and explain it.
ii.Solving the recurrence relation to show that the complexity of your algorithm is O(log n) (for simplicity,you can assume that n = 2k).
Consider the following problem.
Given an array A consisting of n distinct integers A[0],… A[n-1].It is known that there is a position p between 1 and n,such that A[0] A[n-1] .
1.Design adivide-and-conquer algorithmto find the position p with running time of O(log n) in the worst case
2.Justify the running time of your algorithm by
i.Finding the recurrence relation for its time complexity and explain it.
ii.Solving the recurrence relation to show that the complexity of your algorithm is O(log n) (for simplicity,you can assume that n = 2k).
▼优质解答
答案和解析
分治只是表现嘛,本质是递归recursion才对.
代码如下:
查找位置p(数组A){
v = A[A.length/2]
如果v比左侧小,查找位置p(左侧)
如果v比右侧小,查找位置p(右侧)
否则返回v
}
代码如下:
查找位置p(数组A){
v = A[A.length/2]
如果v比左侧小,查找位置p(左侧)
如果v比右侧小,查找位置p(右侧)
否则返回v
}
看了 学过分而治之算法的人进来看看...的网友还看了以下:
已知集合A={x/x=3n+1,n∈Z}B={x/x=3n+2,n∈Z}M={x/x=6n+3,n 2020-04-05 …
已知集合A={x/x=3n+1,n∈Z}B={x/x=3n+2,n∈Z}M={x/x=6n+3,n 2020-04-05 …
集合A={x/x=3n+1,n∈Z}B={x/x=3n+2,n∈Z}M={x/x=6n+3,n∈Z 2020-04-05 …
设f(x)是定义在对称区间(-L,L)内的任何函数,证明……设f(x)是定义在对称区间(-L,L) 2020-05-16 …
写出下列算法的功能LinkListdemo(LinkListL){ListNode*q,*p;If 2020-05-17 …
设函数f(x)是定义在(-L,L)内的奇函数(L〉0),证明若f(x)在(-L,0)内单调增加,则 2020-06-09 …
定义点P(x0,y0)到直线l:Ax+By+C=0(A2+B2≠0)的有向距离为d=Ax0+By0 2020-07-09 …
已知一个边长为a的等边三角形,现将其边长n(n为大于2的整数)等分,并以相邻等分点为顶点向外作小等 2020-08-01 …
设l,m,n为三条不同的直线,a为一个平面,对于下列命题:①若l⊥a,则l与a相交;②若m⊂a,n⊂ 2020-11-02 …
用L,l,m表示k某种弹簧秤原来的长度为l,悬挂重物后的长度L可用公式L=l+k分之m表示某种弹簧秤 2020-12-05 …