1. Time Complexity
1. Complexity: A measure of the efficiency of an algorithm
// Please calculate how many times the func1 basic operation has been performed? void func1(int N){ int count = 0; for (int i = 0; i < N ; i++) { for (int j = 0; j < N ; j++) {//N*N count++; } } for (int k = 0; k < 2 * N ; k++) {//2N count++; } int M = 10; while ((M--) > 0) {//10 count++; } System.out.println(count); }
Calculated: func1 basic operation performed (N^2+2N+10)
// Calculate the time complexity of func2? void func2(int N) { int count = 0; for (int k = 0; k < 2 * N ; k++) {//2N count++; } int M = 10; while ((M--) > 0) {//10 count++; } System.out.println(count); } //2N+10->N
So its time complexity is O(N)
Example 2:
// Calculate the time complexity of func3? void func3(int N, int M) { int count = 0; for (int k = 0; k < M; k++) {//M count++; } for (int k = 0; k < N ; k++) {//N count++; } System.out.println(count); }
M and N are both unknown, so their time complexity is O (M+N)
Example 3:
// Calculate the time complexity of func4? void func4(int N) { int count = 0; for (int k = 0; k < 100; k++) {//100 count++; } System.out.println(count); } //100->1
All constant terms are replaced by 1, so its time complexity is O(1)
Example 4 (bubble sort):
// Calculate the time complexity of bubbleSort? void bubbleSort(int[] array) { for (int end = array.length; end > 0; end--) {//N boolean sorted = true; for (int i = 1; i < end; i++) {//N(N-1) if (array[i - 1] > array[i]) { Swap(array, i - 1, i); sorted = false; } } if (sorted == true) { break; } } } //N(N-1)=N^2-N->N^2
So its time complexity is O (N^2), which is best of all its time complexity. Its best time complexity is O (N), which is equivalent to ordering i only once.
Example 5 (Binary Search):
// Compute the time complexity of 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; }
// Calculate the time complexity of factorial recursion factorial? long factorial(int N) { return N < 2 ? N : factorial(N-1) * N; }
// Calculate the time complexity of fibonacci recursion? int fibonacci(int N) { return N < 2 ? N : fibonacci(N-1)+fibonacci(N-2); }
2. Spatial Complexity
1. Spatial Complexity measures the extra space required by an algorithm. Spatial Complexity counts the number of variables
2. Representation: expressed by large O asymptotic method
3. Examples of common spatial complexity algorithms
Example 1:
// Calculate the spatial complexity of bubbleSort? void bubbleSort(int[] array) {//1 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; } } } // Compute the spatial complexity of fibonacci? int[] f
// Compute the spatial complexity of 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; }
fibonacci has N spaces to store each recursive value, so its spatial complexity is O(N)
Example 3:
// Calculate the time complexity of factorial recursive Factorial? long factorial(int N) { return N < 2 ? N : factorial(N-1)*N; }
N calls were made recursively, opening up N stack frames, each using N constant spaces. Spatial complexity is O(N)