Java - Time Complexity and Space Complexity

The time complexity and space complexity of commonly used sorting algorithms are as follows:
insert image description here

1. Algorithmic efficiency

There are two types of algorithm efficiency analysis:
the first is time efficiency, and the second is space efficiency. Time efficiency is called time complexity, while space efficiency is called space complexity. Time complexity mainly measures the running speed of an algorithm, while space complexity mainly measures the extra space required by an algorithm. In the early days of computer development, the storage capacity of computers was very small. So it is very concerned about the space complexity. But after the rapid development of the computer industry, the storage capacity of the computer has reached a very high level. So we no longer need to pay special attention to the space complexity of an algorithm

2. Time complexity

2.1 The concept of time complexity

The time spent by an algorithm is proportional to the number of executions of the statements in it, and the number of executions of the basic operations in the algorithm is the time complexity of the algorithm.

2.2 Asymptotic Notation for Big O

// 请计算一下func1基本操作执行了多少次?
void func1(int N){
    
    
    int count = 0;
    for (int i = 0; i < N ; i++) {
    
    
        for (int j = 0; j < N ; j++) {
    
    
        count++;
        }
    }
    for (int k = 0; k < 2 * N ; k++) {
    
    
       count++;
    }
    int M = 10;
    while ((M--) > 0) {
    
    
        count++;
    }
    System.out.println(count);
}

The number of basic operations performed by Func1:
insert image description here
In practice, when we calculate the time complexity, we do not necessarily need to calculate the exact number of executions, but only the approximate number of executions, so here we use the big-O asymptotic notation
Big O notation (Big O O notation): is a mathematical notation used to describe the asymptotic behavior of a function.
Principles of the Big O-order method :
1. Replace all additive constants in running time with the constant 1.
2. In the modified run times function, only the highest-order term is retained.
3. If the highest-order term exists and is not 1, remove the constant multiplied by this term. The result is a big-O order
.
After using the asymptotic representation of big O, the time complexity of Func1 is:
insert image description here
from the above we will find that the asymptotic representation of big O removes those items that have little effect on the result, and shows the number of executions succinctly and clearly.
In addition, the time complexity of some algorithms has a best, average and worst case:
worst case: the maximum number of runs (upper bound) for
any input size Average case: the desired number of runs for
any input size Best case: for any input size Minimum number of runs (lower bound)
For example : search for a data x in an array of length N
Best case: 1 finding
Worst case: N finding
Average case: N/2 finding
In practice, the general case is the algorithm The worst-case operation of , so the time complexity of searching for data in the array is O(N)

2.3 Common time complexity calculation examples

Example 1:

// 计算func2的时间复杂度?
void func2(int N) {
    
    
    int count = 0;
    for (int k = 0; k < 2 * N ; k++) {
    
    
     count++;
    }
    int M = 10;
    while ((M--) > 0) {
    
    
     count++;
    }
    System.out.println(count);
}

Example 2:

// 计算func3的时间复杂度?
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++;
    }
    System.out.println(count);
}

Example 3:

// 计算func4的时间复杂度?
void func4(int N) {
    
    
    int count = 0;
    for (int k = 0; k < 100; k++) {
    
    
    count++;
    }
    System.out.println(count);
}

Example 4:

// 计算bubbleSort的时间复杂度?
void bubbleSort(int[] array) {
    
    
    for (int end = array.length; end > 0; end--) {
    
    
    boolean sorted = true;
    for (int i = 1; i < end; i++) {
    
    
        if (array[i - 1] > array[i]) {
    
    
        Swap(array, i - 1, i);
        sorted = false;
        }
    }
        if (sorted == true) {
    
    
            break;
        }
    }
}

Example 5:

// 计算binarySearch的时间复杂度?
int binarySearch(int[] array, int value) {
    
    
    int begin = 0;
    int end = array.length - 1;
    while (begin <= end) {
    
    
        int mid = begin + ((end-begin) / 2);
        if (array[mid] < value)
            begin = mid + 1;
        else if (array[mid] > value)
            end = mid - 1;
        else
        return mid;
    }
    return -1;
}

Example 6:
Note:
The time complexity of recursion = the number of recursion * the number of times each recursion is executed

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

Example 7:

// 计算斐波那契递归fibonacci的时间复杂度?
int fibonacci(int N) {
    
    
    return N < 2 ? N : fibonacci(N-1)+fibonacci(N-2);
}

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. Example 2 The basic operation 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 performed 100 times. By deriving the big O-order method, the time complexity is O(1)
  4. Example 4 The basic operation is best performed N times, and the worst is performed (N*(N-1))/2 times. By deriving the big O-order method + the time complexity is generally the worst, and the time complexity is O(N^ 2)
  5. Example 5 The basic operation is best performed once, and the worst is O(logN) times, and the time complexity is O(logN) ps: logN means that the base is 2 and the logarithm is N in the algorithm analysis. In some places it will be written as lgN. (It is recommended to explain how logN is calculated by means of origami search) (because the binary search eliminates half of the unsuitable values ​​each time, once the two points are left: n/2 twice the two points are left: n/2/2 = n /4)
  6. In Example 6, it is found through calculation and analysis that the basic operations recurse N times, and the time complexity is O(N).
  7. Example 7 shows that the basic operation recurses 2^N times through computational analysis, and the time complexity is O( 2^N). (It is recommended to explain the binary tree of the recursive stack frame for drawing)

3. Space complexity

Space complexity is a measure of the amount of storage space temporarily . The space complexity is not how many bytes the program occupies, because this does not make much sense, so the space complexity is the number of variables . The space complexity calculation rules are basically similar to the practical complexity, and also use the big-O asymptotic notation.

3.1 Common time complexity calculation examples

Example 1:

// 计算bubbleSort的空间复杂度?
void bubbleSort(int[] array) {
    
    
    for (int end = array.length; end > 0; end--) {
    
    
    boolean sorted = true;
    for (int i = 1; i < end; i++) {
    
    
        if (array[i - 1] > array[i]) {
    
    
        Swap(array, i - 1, i);
        sorted = false;
        }
    }
        if (sorted == true) {
    
    
            break;
        }
    }
}

Example 2:

// 计算fibonacci的空间复杂度?
int[] fibonacci(int n) {
    
    
    long[] fibArray = new long[n + 1];
    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 factorial(int 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, opens up N stack frames, and each stack frame uses a constant amount of space. The space complexity is O(N)

Guess you like

Origin blog.csdn.net/qq_43398758/article/details/121063175