Algorithm 平均情况下的算法分析

Algorithm 平均情况下的算法分析,algorithm,complexity-theory,calculus,Algorithm,Complexity Theory,Calculus,我试图解决一个非常简单的算法分析(显然对我来说不是那么简单) 算法如下所示: int findIndexOfN(int A[], int n) { // this algorithm looks for the value n in array with size of n. // returns the index of n in case found, otherwise returns -1. // it is possible that n doesn't appear in the a

我试图解决一个非常简单的算法分析(显然对我来说不是那么简单)

算法如下所示:

int findIndexOfN(int A[], int n) {
// this algorithm looks for the value n in array with size of n.
// returns the index of n in case found, otherwise returns -1.
// it is possible that n doesn't appear in the array.
// n appears at most one time.
// the probability that n doesn't appear in the array is $1/(n+1)$
// for each cell in the array, the probability that n is found in index i
// is $1/(n+1)$


    int index, fIndex;
    index = 0;
    fIndex = -1;
    while (index < n && fIndex == -1) {
      if(A[index] == n) {
        fIndex = index;
      }
      index++;
    }
    return fIndex;

 }
for (int i = 0; i < n; ++i) {
    if (A[i] == n)
        return i;
}
intfindindexofn(inta[],intn){
//该算法在大小为n的数组中查找值n。
//如果找到,则返回n的索引,否则返回-1。
//可能n没有出现在数组中。
//n最多出现一次。
//n不出现在数组中的概率为$1/(n+1)$
//对于数组中的每个单元格,在索引i中找到n的概率
//是$1/(n+1)$
int索引,fIndex;
指数=0;
fIndex=-1;
而(索引
现在我正在计算平均运行时间。我认为这是几何级数,但我找不到一种方法来合并概率和复杂性这两个术语

例如,我知道如果在索引1中找到值n,则需要1个循环步骤才能得到第二个索引(1)并找到n

另一方面,概率给了我一些分数

以下是我到目前为止得到的信息:

$\sigma从i=1到n评估((1/n)*((n-1)/n)^i-1)

但同样,我不能找到这个公式与t(n)的关系,也不能找到这个函数的BigOh,BigOmega或θ的关系。

这个算法是BigOh(n),BigOmega(n)和θ(n)

要知道这一点,您不需要计算概率或使用主定理(因为您的函数不是递归的)。您只需要看到函数就像是
n
术语上的循环。如果您这样表示您的函数,可能会更容易:

int findIndexOfN(int A[], int n) {
// this algorithm looks for the value n in array with size of n.
// returns the index of n in case found, otherwise returns -1.
// it is possible that n doesn't appear in the array.
// n appears at most one time.
// the probability that n doesn't appear in the array is $1/(n+1)$
// for each cell in the array, the probability that n is found in index i
// is $1/(n+1)$


    int index, fIndex;
    index = 0;
    fIndex = -1;
    while (index < n && fIndex == -1) {
      if(A[index] == n) {
        fIndex = index;
      }
      index++;
    }
    return fIndex;

 }
for (int i = 0; i < n; ++i) {
    if (A[i] == n)
        return i;
}
为什么??因为如果
n
位于第一个位置(概率
1/(n+1)
),则需要进行一次测试,如果
n
位于第二个位置(概率
1/(n+1)
),则需要进行两次测试
i
测试
n
是否处于
i
th位置(概率
1/(n+1)

本系列的计算结果为

n(n+1)/2 * 1/(n+1) = n/2
该算法是BigOh(n)、BigOmega(n)和θ(n)

要知道这一点,您不需要计算概率或使用主定理(因为您的函数不是递归的)。您只需要看到函数就像是
n
术语上的循环。如果您这样表示您的函数,可能会更容易:

int findIndexOfN(int A[], int n) {
// this algorithm looks for the value n in array with size of n.
// returns the index of n in case found, otherwise returns -1.
// it is possible that n doesn't appear in the array.
// n appears at most one time.
// the probability that n doesn't appear in the array is $1/(n+1)$
// for each cell in the array, the probability that n is found in index i
// is $1/(n+1)$


    int index, fIndex;
    index = 0;
    fIndex = -1;
    while (index < n && fIndex == -1) {
      if(A[index] == n) {
        fIndex = index;
      }
      index++;
    }
    return fIndex;

 }
for (int i = 0; i < n; ++i) {
    if (A[i] == n)
        return i;
}
为什么??因为如果
n
位于第一个位置(概率
1/(n+1)
),则需要进行一次测试,如果
n
位于第二个位置(概率
1/(n+1)
),则需要进行两次测试
i
测试
n
是否处于
i
th位置(概率
1/(n+1)

本系列的计算结果为

n(n+1)/2 * 1/(n+1) = n/2

我最需要的是平均跑步时间。我想知道如何做到这一点,尽管我同意你的说法,这听起来像n。我在课堂上看到一些平均案例运行时间变为logn的例子。我只是想了解正确的形式。首先,这是一个很好的观察。我只是需要一些正式的解释。还有一件事。那n不出现的机会呢?你忽略了,对吗?我的意思是,这些信息是有目的的,或者我可以忽略它?是的,我忘了。这将增加n/(n+1)到序列中,这将被忽略。1/(n+1)+2/(n+1)+3/(n+1)n/(n+1)=我主要需要平均运行时间。我想知道如何做到这一点,尽管我同意你的说法,这听起来像n。我在课堂上看到一些平均案例运行时间变为logn的例子。我只是想了解正确的形式。首先,这是一个很好的观察。我只是需要一些正式的解释。还有一件事。那n不出现的机会呢?你忽略了,对吗?我的意思是,这些信息是有目的的,或者我可以忽略它?是的,我忘了。这将增加n/(n+1)到序列中,这将被忽略。1/(n+1)+2/(n+1)+3/(n+1)n/(n+1)=?