早教吧作业答案频道 -->英语-->
学过分而治之算法的人进来看看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
}
看了 学过分而治之算法的人进来看看...的网友还看了以下:
matlab解一个二元方程组为何出现错误.方程组为0.0231=d+p0.0284=0.0231+ 2020-05-16 …
方程x2+y2+Dx+Ey+F=0(D2+E2-4F>0)表示的曲线关于x+y=0成轴对称图形,则 2020-07-09 …
方程x2+y2+Dx+Ey+F=0(D2+E2-4F>0)表示的曲线关于x+y=0成轴对称图形,则 2020-07-09 …
设A是如下形式的2行3列的数表,满足性质P:a,b,c,d,e,f∈[-1,1],且a+b+c+d+ 2020-11-17 …
设A是如下形式的2行3列的数表,abcdef满足性质P:a,b,c,d,e,f,且a+b+c+d+e 2020-11-17 …
设A是如下形式的2行3列的数表,abcdef满足性质P:a,b,c,d,e,f∈[-1,1],且a+ 2020-11-17 …
(2012•北京)设A是如下形式的2行3列的数表,abcdef满足性质P:a,b,c,d,e,f∈[ 2020-11-17 …
求解多元一次不等式的编程47a-b-c-d-e-f-g>047b-a-c-d-e-f-g>023c- 2020-12-14 …
大学文科高数概率题~1.某人打靶的命中率为,现独立地射击5次,那么5次射击中命中2次的概率为多少?2 2020-12-14 …
c++关于指针(8)已知数组A和指针p定义为:intA[20][10],*p;,要使p指向A的首元素 2020-12-31 …