# Time complexity and space complexity

Keywords: Java data structure JavaSE

## Algorithm efficiency

There are two kinds of algorithm efficiency analysis:

1. Time efficiency,
2. Space efficiency.

Time efficiency - > time complexity
Space efficiency - > space complexity.
Time complexity mainly measures the running speed of an algorithm, while space complexity mainly measures the additional space required by an algorithm
In the early days of computer development, the storage capacity of computer was very small. So I care about the complexity of space. However, with the rapid development of computer industry, the storage capacity of computer has reached a high level. Therefore, we no longer need to pay special attention to the spatial complexity of an algorithm

### Time complexity

Definition of time complexity: in computer science, the time complexity of an algorithm is a function that quantitatively describes the running time of the algorithm. In theory, the time spent in the execution of an algorithm cannot be calculated. You can only know when you put your program on the machine and run it. But do we need every algorithm to be tested on the computer? Yes, it can be tested on the computer, but it is very troublesome, so there is the analysis method of time complexity. The time spent by an algorithm is directly proportional to the execution times of the statements in it. The execution times of the basic operations in the algorithm is the time complexity of the algorithm.

The asymptotic representation of large O is a mathematical symbol used to describe the asymptotic behavior of a function

1. Replace all addition constants in the run time with 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, the constant multiplied by this item is removed. The result is large O-order.

In practice, we usually focus on the worst-case operation of the algorithm, so the time complexity of searching data in the array is O(N)

### Calculate the time complexity of the following programs

```public class TestFunc {

// Please calculate how many times func1 basic operations have been performed?
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);
// After using the asymptotic representation of large o, the time complexity of Func1 is O(N^2)
}
}
```
```public class TestFunc {

// Calculate the time complexity of 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);
// O(N)
}
}
```
```public class TestFunc {

// Calculate the time complexity of 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);
// O(M+N)
}
}
```
```public class TestFunc {

// Calculate the time complexity of func4?
void func4(int N) {
int count = 0;
for (int k = 0; k < 100; k++) {
count++;
}
System.out.println(count);
// O(1)
}
}
```
```public class TestFunc {

// Calculate the time complexity of 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;
}
}
// n*(n-1) --> O(N^2)
}
}
```
```public class TestFunc {

// Calculate 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;
}
}
``` ```public class TestFunc {

// The time complexity of computing factorial recursive factorial?
long factorial(int N) {
// The time complexity of recursion = the number of recursions and the number of times each pass is executed
//                                     1
// O(N)
return N < 2 ? N : factorial(N-1) * N;
}
}
```
```public class TestFunc {

// Computing the time complexity of fibonacci recursive fibonacci?
int fibonacci(int N) {
return N < 2 ? N : fibonacci(N-1)+fibonacci(N-2);
//
}
}
``` ## Spatial complexity

Space complexity is a measure of the amount of storage space temporarily occupied by an algorithm during operation. Space complexity is not how many bytes the program occupies, because it doesn't make much sense, so space complexity is the number of variables. The calculation rules of spatial complexity are basically similar to the practical complexity, and the large O asymptotic representation is also used

### Calculate the following program space complexity

```public class TestFunc {

// Calculate the spatial complexity of bubbleSort1?
void bubbleSort1(int[] array) {
for (int end = array.length; end > 0; end--) {
boolean sorted = true; // 1
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 extra spaces are used, so the space complexity is O(1)
}
}
```
```public class TestFunc {

// How to calculate the spatial complexity of fibonacci?
int[] fibonacci(int n) {
long[] fibArray = new long[n + 1]; // n+1 --> O(N)
// Dynamic opening up N spaces
fibArray = 0;
fibArray = 1;
for (int i = 2; i <= n ; i++) {
fibArray[i] = fibArray[i - 1] + fibArray [i - 2];
}
return fibArray;
}
}
```
```public class TestFunc {

// Calculate the spatial complexity of Factorial recursive Factorial?
long factorial(int N) {
return N < 2 ? N : factorial(N-1)*N;
// Each recursion opens up a block of memory
// Recursively called N times, opened up N stack frames, and each stack frame uses a constant space. Space complexity is O(N)
}
}
```

Posted by siko on Wed, 01 Dec 2021 01:27:13 -0800