📜  算法分析|第2组(最差,平均和最佳情况)

📅  最后修改于: 2021-05-08 17:46:31             🧑  作者: Mango

在上一篇文章中,我们讨论了渐近分析如何克服天真的分析算法方法的问题。在本文中,我们将以线性搜索为例,并使用渐近分析对其进行分析。
我们可以通过三种情况来分析算法:
1)最坏的情况
2)平均情况
3)最好的情况
让我们考虑以下线性搜索的实现。

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Linearly search x in arr[].
// If x is present then return the index,
// otherwise return -1
int search(int arr[], int n, int x)
{
    int i;
    for (i = 0; i < n; i++) {
        if (arr[i] == x)
            return i;
    }
    return -1;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 10, 30, 15 };
    int x = 30;
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << x << " is present at index "
         << search(arr, n, x);
  
    getchar();
    return 0;
}
  
// This code is contributed
// by Akanksha Rai


C
// C implementation of the approach
#include 
  
// Linearly search x in arr[].
// If x is present then return the index,
// otherwise return -1
int search(int arr[], int n, int x)
{
    int i;
    for (i = 0; i < n; i++) {
        if (arr[i] == x)
            return i;
    }
    return -1;
}
  
/* Driver program to test above functions*/
int main()
{
    int arr[] = { 1, 10, 30, 15 };
    int x = 30;
    int n = sizeof(arr) / sizeof(arr[0]);
    printf("%d is present at index %d", x,
           search(arr, n, x));
  
    getchar();
    return 0;
}


Java
// Java implementation of the approach
  
public class GFG {
  
    // Linearly search x in arr[].  If x is present then
    // return the index, otherwise return -1
    static int search(int arr[], int n, int x)
    {
        int i;
        for (i = 0; i < n; i++) {
            if (arr[i] == x) {
                return i;
            }
        }
        return -1;
    }
  
    /* Driver program to test above functions*/
    public static void main(String[] args)
    {
        int arr[] = { 1, 10, 30, 15 };
        int x = 30;
        int n = arr.length;
        System.out.printf("%d is present at index %d", x,
                          search(arr, n, x));
    }
}
  
/*This code is contributed by PrinciRaj1992*/


Python3
# Python 3 implementation of the approach
  
# Linearly search x in arr[]. If x is present
# then return the index, otherwise return -1
  
  
def search(arr, x):
    for index, value in enumerate(arr):
        if value == x:
            return index
    return -1
  
  
# Driver Code
arr = [1, 10, 30, 15]
x = 30
print(x, "is present at index",
      search(arr, x))
  
# This code is contributed
# by PrinciRaj1992


C#
// C# implementation of the approach
using System;
public class GFG {
  
    // Linearly search x in arr[].  If x is present then
    // return the index, otherwise return -1
    static int search(int[] arr, int n, int x)
    {
        int i;
        for (i = 0; i < n; i++) {
            if (arr[i] == x) {
                return i;
            }
        }
        return -1;
    }
  
    /* Driver program to test above functions*/
    public static void Main()
    {
        int[] arr = { 1, 10, 30, 15 };
        int x = 30;
        int n = arr.Length;
        Console.WriteLine(x + " is present at index "
                          + search(arr, n, x));
    }
}
  
/*This code is contributed by PrinciRaj1992*/


PHP


输出:

30 is present at index 2

最坏情况分析(通常完成)
在最坏的情况下,我们计算算法运行时间的上限。我们必须知道导致最大数量的操作执行的情况。对于线性搜索,当数组中不存在要搜索的元素(上述代码中的x)时,最坏的情况发生。当x不存在时,search()函数将其与arr []的所有元素一一比较。因此,线性搜索的最坏情况下的时间复杂度将为Θ(n)。

平均案例分析(有时完成)
在平均案例分析中,我们采用所有可能的输入并计算所有输入的计算时间。将所有计算出的值求和,然后将总和除以输入总数。我们必须知道(或预测)案件的分布。对于线性搜索问题,让我们假设所有情况都是均匀分布的(包括x不在数组中的情况)。因此,我们将所有情况相加,然后将总和除以(n + 1)。以下是平均案例时间复杂度的值。

平均案件时间= 分析1 = 分析2 =Θ(n)

最佳案例分析(伪造)
在最佳情况下,我们计算算法运行时间的下限。我们必须知道导致最少数量的操作执行的情况。在线性搜索问题中,最好的情况是x在第一个位置出现。在最佳情况下,操作数是恒定的(不依赖于n)。因此,最佳情况下的时间复杂度为Θ(1)
在大多数情况下,我们会进行最坏情况分析来分析算法。在最坏的分析中,我们保证算法运行时间的上限,这是一个很好的信息。
在大多数实际案例中,平均案例分析并不容易,而且很少进行。在平均案例分析中,我们必须知道(或预测)所有可能输入的数学分布。
最佳案例分析是虚假的。确保算法的下限不会提供任何信息,因为在最坏的情况下,算法可能需要数年才能运行。
对于某些算法,所有情况在渐近上都是相同的,即,没有最坏的情况和最好的情况。例如,合并排序。在所有情况下,合并排序都会执行Θ(nLogn)操作。其他大多数排序算法都有最坏的情况和最好的情况。例如,在“快速排序”的典型实现中(将枢轴选择为边角元素),当输入数组已被排序时,最坏的情况发生;当枢轴元素始终将数组分为两半时,最坏的情况发生。对于插入排序,最坏的情况发生在对数组进行反向排序时,最坏的情况发生在以与输出相同的顺序对数组进行排序时。