Time and space complexity calculation

content

Objectives of this chapter

Understand time and space complexity

Computational time complexity

Computational space complexity

calculation exercise

        1.1 Common time complexity calculation examples

Example 1

Example 2

Example 3

Example 4

Example 5

1.2 Calculation of common space complexity

Example 1

Example 2

Example 3


Objectives of this chapter

1. What is time complexity and space complexity.

2. Why have time complexity and space complexity.

3. How to calculate time and space complexity.

4. Common Complexity Calculation Exercises

Understand time and space complexity

Time Complexity: A measure of how fast an algorithm runs.

Space complexity: an indicator that measures the amount of storage space temporarily occupied by program running.

Why have time complexity and space complexity?

Time complexity and space complexity can help us measure the pros and cons of an algorithm. In different environments, we have different requirements for time efficiency and space efficiency. Therefore, the time complexity and space complexity can guide programmers to design in line with the program environment. code.

Computational time complexity

The calculation of time complexity does not require us to calculate the exact running time of the algorithm. Since the running time is proportional to the number of executions, we use the number of executions of the algorithm as the time complexity calculation.

In addition, the time complexity of some algorithms has the best, average and worst cases, and we take the worst case as the time complexity.

Note: The worst case represents the maximum number of executions of the algorithm, usually with conditional judgment statements.

Explain why: Average case = (worst case + best case)/2 = worst case/2 + best case/2

The average case equals the worst case according to the asymptotic notation of Big O. (similar to taking the limit)

Computational space complexity

The calculation of space complexity is based on the number of application variables.

calculation exercise

1.1 Common time complexity calculation examples

Example 1

void Func2(int N)
{
    int count = 0;
    for (int k = 0; k < 2 * N ; ++ k)
    {
        ++count;
    }
        int M = 10;
    while (M--)
    {
        ++count;
    }
    printf("%d\n", count);
}

Example 2

void Func3(int N, int M)
{
    int count = 0;
    for (int k = 0; k < M; ++ k)
    {
        ++count;
    }
    for (int k = 0; k < N ; ++ k)
    {
        ++count;
    }
    printf("%d\n", count);
}

Example 3

// 计算BinarySearch的时间复杂度?
int BinarySearch(int* a, int n, int x)
{
    assert(a);
    int begin = 0;
    int end = n;
    while (begin < end)
    {
        int mid = begin + ((end-begin)>>1);
        if (a[mid] < x)
            begin = mid+1;
        else if (a[mid] > x)
            end = mid;
        else
            return mid;
    }
    return -1;
}

Example 4

// 计算阶乘递归Factorial的时间复杂度?
long long Factorial(size_t N)
{
    return N < 2 ? N : Factorial(N-1)*N;
}

Example 5

// 计算BubbleSort的时间复杂度?
void BubbleSort(int* a, int n)
{
    assert(a);
    for (size_t end = n; end > 0; --end)
    {
        int exchange = 0;
        for (size_t i = 1; i < end; ++i)
            {
                if (a[i-1] > a[i])
                {
                    Swap(&a[i-1], &a[i]);
                    exchange = 1;
                }
            }
                if (exchange == 0)
                    break;
    }
}

Example answers and analysis

1. The basic operation of example 1 is performed 2N+10 times. By deriving the big O-order method, we know that the time complexity is O(N)

2. The basic operation of example 2 is performed M+N times, there are two unknowns M and N, and the time complexity is O(N+M)
3. The basic operation of example 3 is best performed once, and the worst is O(logN) times , the time complexity is O(logN) ps: logN in the algorithm analysis
means that the base is 2 and the logarithm is N. In some places it will be written as lgN.

4. In Example 4, it is found through calculation and analysis that the basic operations recurse N times, and the time complexity is O(N).

5. The basic operation of example 5 is best performed N times, and the worst is performed (N*(N+1)/2 times. By deriving the big O-order method + time complexity is
generally considered the worst, and the time complexity is O(N ^2)

1.2 Calculation of common space complexity
Example 1

// 计算BubbleSort的空间复杂度?
void BubbleSort(int* a, int n)
{
    assert(a);
    for (size_t end = n; end > 0; --end)
    {
        int exchange = 0;
        for (size_t i = 1; i < end; ++i)
        {
            if (a[i-1] > a[i])
            {
                Swap(&a[i-1], &a[i]);
                exchange = 1;
            }
        }
        if (exchange == 0)
            break;
    }
}

Example 2

// 计算Fibonacci的空间复杂度?
long long* Fibonacci(size_t n)
{
    if(n==0)
    return NULL;
    long long * fibArray = (long long *)malloc((n+1) * sizeof(long long));
    fibArray[0] = 0;
    fibArray[1] = 1;
    for (int i = 2; i <= n ; ++i)
    {
        fibArray[i ] = fibArray[ i - 1] + fibArray [i - 2];
    }
    return fibArray ;
}

Example 3

// 计算阶乘递归Factorial的空间复杂度?
long long Factorial(size_t N)
{
    return N < 2 ? N : Factorial(N-1)*N;
}

Example answers and analysis:
1. Example 1 uses a constant amount of extra space, so the space complexity is O(1)
2. Example 2 dynamically opens up N spaces, and the space complexity is O(N)
3. Example 3 recursively calls N times, N stack frames are opened, and each stack frame uses a constant amount of space. The space complexity is O(N)

 

Guess you like

Origin blog.csdn.net/m0_62171658/article/details/123228832