catalogue

Asymptotic representation of large O

Common time complexity calculation examples

Examples of common space complexity calculation

# preface

This chapter mainly explains:

- Explanation of time complexity and space complexity
- Common complexity related exercises

# Algorithm efficiency

- Criteria for measuring the quality of an algorithm:

# Time complexity

- Concept:

## Asymptotic representation of large O

- Derivation of large O-order method:

- Replace all addition constants in the run time with constant 1
- In the modified run times function, only the highest order term is retained
- If the highest order term exists and is not 1, remove the constant multiplied by this item, and the result is large O-order

- In short:

- Example:

void Func(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--) { ++count; } printf("%d\n", count); }

Number of basic operations performed:

- be careful:

- Example: search for a data x in an array of length N

## Common time complexity calculation examples

- Example 1:

void Func1(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 Func2(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); }

Time complexity: O(N+M)

- Example 3:

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

Time complexity: O(1)

Note: it is not executed once, but represents constant times

- Example 4:

// The time complexity of calculating strchr? //Find a character in the string and return the corresponding address (similar to the traversal algorithm) const char * strchr ( const char * str, int character );

Time complexity: O(N)

- Example 5:

// Calculate the time complexity of 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; } }

Execution times expression: n-1+n-2+n-3+...+1=n (n-1) / 2 (sum of equal differences)

Time complexity: O(N^2)

- Example 6:

// Calculate the time complexity of BinarySearch? int BinarySearch(int* a, int n, int x) { assert(a); int begin = 0; int end = n-1; 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; }

Execution times expression:

Note: x is the search times in the worst case, and N is the array length

Reverse thinking: push back from finding. The number of each push back is x2. After x times, the final total number is the total length of the array

The time complexity is:

- Example 7:

// The time complexity of computing factorial recursive Fac? long long Fac(size_t N) { if(0 == N) return 1; return Fac(N-1)*N; }

Number of executions expression: n

Time complexity: O(N)

- Example 8:

// The time complexity of computing Fibonacci recursive Fib? long long Fib(size_t N) { if(N < 3) return 1; return Fib(N-1) + Fib(N-2); }

Note: in fact, the call on the right will end faster than that on the left, that is, some items will be missing on the right

Execution times expression:(equal ratio summation, C is constant)

The time complexity is:

# Spatial complexity

- Concept:

## Examples of common space complexity calculation

- Example 1:

// Calculate the spatial complexity of 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:

// Calculate the spatial complexity of Fibonacci? // Returns the first n items of the Fibonacci sequence 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; }

Note: the space complexity depends on the additional space consumption. When the Fibonacci sequence branch calls to the end, it starts to return, and the return will destroy the function stack frame, so at most N function stack frames (space) can be opened up

- Example 3:

// Calculate the spatial complexity of factorial recursive Fac? long long Fac(size_t N) { if(N == 0) return 1; return Fac(N-1)*N; }

Note: N recursive calls are made, and N additional function stack frames (space) are opened up