【Data structure】_1. Collection and complexity

Table of contents

1. Collection framework

2. Time complexity

2.1 Time Complexity and Space Complexity

2.2 The concept of time complexity

2.3 Asymptotic representation of big O

2.3.1 Exact Time Complexity Expression

2.3.2 Three Rules of Big O Asymptotic Notation

2.3.3 Best, average and worst case time complexity

2.4 Example of Time Complexity Calculation

3. Space complexity


1. Collection framework

The Java collection mining machine, also known as the collection container, is a set of interfaces and its implementation classes defined under the java.util package.

The main performance is to place multiple elements in the same unit for fast and convenient storage, retrieval, and management of these elements;

2. Time complexity

2.1 Time Complexity and Space Complexity

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 and space efficiency is called space complexity. Time complexity mainly measures the running speed of the algorithm, and space complexity mainly measures the extra space of the algorithm.

With the rapid development of computers, the storage capacity of computers has reached a very high level. At present, the important criterion for measuring the pros and cons of an algorithm is time complexity;

2.2 The concept of time complexity

In computer science, the time complexity of an algorithm is a mathematical function that quantitatively describes the running time of that algorithm.

The time spent by an algorithm is proportional to the number of executions of its statement, and the number of executions of basic operations in the algorithm is the time complexity of the algorithm;

2.3 Asymptotic representation of big O

2.3.1 Exact Time Complexity Expression

Try to analyze the time complexity of the following code:

    public static void fun1(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++;
        }
    }

 analyse as below:

The exact time complexity expression is: F(N) = N^2 + 2*N + 10;

In fact, when we calculate the time complexity, we do not need the exact number of executions, but only the approximate number of executions, so we introduce the big O progressive notation;

2.3.2 Three Rules of Big O Asymptotic Notation

(The expression in parentheses corresponds to the simplification process of the above expression)

(1) Replace all additive constants in runtime with a constant 1; (F(N) = N^2 + 2*N + 1)

(2) In the modified function of the number of runs, only the highest-order term is kept; (F(N) = N^2 )

(3) If the highest-order item exists and is not 1, remove the constant multiplied by this item;

(If the expression is F(N) = 3*N^2, remove 3 and change to F(N) = N^2)

That is, the big O asymptotic notation removes items that have little effect on the result from the precise time complexity expression, and expresses the number of executions more concisely;

2.3.3 Best, average and worst case time complexity

(1) Worst case: maximum number of runs for any input size (upper bound)

(2) Average case: expected number of runs for any input size

(3) Best case: Minimum number of runs for any input size (lower bound)

The general time complexity refers to the time complexity in the worst case:

For example, to search for an element in an array with a length of N, the best case is found once, the worst case is found N times, and the average case is N/2 times found, but the time complexity of searching data in the array is O (N)

2.4 Example of Time Complexity Calculation

Example 1:

 public static void fun2(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);
    }

The time complexity is O(N);

Example 2:

    public static void fun3(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);
    }

The time complexity is O(M+N);

Example 3:

    public static void fun4(int N){
        int count=0;
        for(int k=0;k<100;k++){
            count++;
        }
        System.out.println(count);
    }

 The time complexity is O(1);

Example 4: (bubble sort)

    public 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;
        }
    }

The calculation process is as follows:

After knowing the total number of executions of the inner and outer loops, keep the highest order item, as follows: the time complexity is O(N^2);

Note: ① N represents the scale of the current problem rather than the variables in the program;

② The best case is that the array itself is ordered, and only needs to be traversed once, and the time complexity is O(N);

③ There is also a slightly different way of writing bubble sort:

public static void bubble_sort(int[] array){
        for(int i=0;i<array.length-1;i++){
            boolean flag = false;
            for(int j=0;j<array.length-1-i;j++){
                if(array[j]>array[j+1]){
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                    flag=true;
                }
            }
            if(flag==false){
                return;
            }
        }
    }

Its computational complexity process is:

Therefore, its time complexity is also O(N^2); 

Example 5: (Binary Search)

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;
    }

The calculation process is as follows:

The enumeration method can also be used: when the number of data is 2, 4, 8, the number of times to be executed is 2, 3, 4 in turn, so there is: 2^(x-1)=N, that is, x=logN+1;

Yes: the time complexity is O(logN); 

Example 6: Calculate the time complexity of factorial recursive factorial:

public static long factorial(int N) {
        return N < 2 ? N : factorial(N-1) * N;
    }

The time complexity of the ternary operator is 1, and it is executed N times, that is, the time complexity is O(N);

Example 7: Calculate the time complexity of Fibonacci recursive fibonacci:

public static int fibonacci(int N) {
        return N < 2 ? N : fibonacci(N-1)+fibonacci(N-2);
    }

The process of finding the time complexity is as follows:

According to the above binary tree, it can be seen that the time complexity is O(2^N);

Note: The time complexity of recursion is the number of recursions * the number of code executions after each recursion ;

3. Space complexity

Space complexity is a measure of the amount of storage space temporarily occupied by an algorithm during operation.

The space complexity is not how many bytes the program occupies, but the number of temporary variables is calculated, and the space will not increase with the increase of the problem scale;

The space complexity is similar to the time complexity calculation method, and the big O asymptotic notation is also used;

Example 1: Bubble sort

    public 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;
        }
    }

A constant number of temporary variables are used, and the space complexity is O(1);

Example 2: Fibonacci Sequence

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;
    }

The program puts each number calculated by the calculated Fibonacci sequence in the fibArray array, so the space complexity is O(N);

Example 3:

long factorial(int N) {
return N < 2 ? N : factorial(N-1)*N;
}

Calling N times will open up N stack frames, and each stack frame uses a constant space, so the space complexity is O(N);

Example 4:

public static int fibonacci(int N) {
        return N < 2 ? N : fibonacci(N-1)+fibonacci(N-2);
    }

The space complexity is O(N);

Guess you like

Origin blog.csdn.net/m0_63299495/article/details/130187754