数组中数字的出现,复杂度为 log n 算法和 c

问题描述

嗨,天才,祝你有美好的一天 情况:

T [M] an array sorts in ascending order.

Write an algorithm that calculates the number of occurrences of an integer x in the array T[M].
the number of comparison of x to the elements of T,must be of order log n

我的尝试是:

def Occurrence(T,X,n):{
   
 
       if ( n=0 ){ return 0;}
        else { 

            Occurrence(T,n/2);

            if( T[n]==X ){  return 1+Occurrence(T,x,n/2); }
            else { return Occurrence(T,n/2); }

 
 }the end of code
complexity is :
                   0 if n=0
we have      O(n)={
                   1+O(n/2) if n>0
O(n)=1+1+1+....+O(n/2^(n))=n+O(2/2^(n))
when algorithm stopp if{existe k  n=2^(k),so   O(n)=n+1 } 
n/2^(n)=1)  =>   O(n)=log(n)+1,so you think my code is true ?
</pre>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)