# Time complexity of algorithm

Posted by ggkfc on Sat, 05 Mar 2022 12:51:38 +0100

# The time of the algorithm is complex

## 1. Time complexity

1. [] the concept of time complexity
2. [~] time complexity rule (representation of big O)
3. [~] common time complex calculations

(1) The concept of time complexity:
The time complexity of the algorithm is a function, which describes the operation time of the algorithm. The time spent by an algorithm is directly proportional to the number of code executions. The execution times of basic operations in the algorithm is the time complexity of the algorithm.
(2) Time complexity rule:

Derivation of large O-order method:

• Use constant 1 to add all constants in jade belt running time.

• In the modified run times function, only the highest order term is retained.

• If the highest order term exists and is not 1, the constant multiplied by the highest item is removed. The result is large order O. After using the asymptotic representation of large O, the time complexity of Func1 becomes O (N^2)

• [ 1 ] N=10 F(N)=100

• [ 2 ] N=100 F(N)=10000

• [ 3 ] N=1000F(N)=1000000

Through the law, we find that the large O asymptotic representation removes the items that have little impact on the results and retains the largest item. In my calculation, we will encounter three situations

1. Best case: the minimum number of runs of any input scale.
2. Average situation: expected number of runs of any input scale
3. Worst case: maximum number of runs of any input scale
For example: find a number X in an array of length N
1. Best case: find it once
2. Average situation: N/2 times
3. Worst case: found N times
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)

(3) The following is an example and calculation of time complexity

Example 1: calculate the total number of times the count statement is executed

```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--)
{
++count;
}
```

By calculating F (N) = N* N+2*N+10;
However, due to the rule O(N^2) of large O progressive representation

• [due to the increase of N, the latter two items have less impact on the whole result.]

• [when N is infinite, the impact of the latter two items on the results can be ignored]

Example 2: the time complexity of calculating 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;
}
printf("%d\n", count);
}
```

The judgment result 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)
{
++count;
}
printf("%d\n", count);
}
```

O(1)

Example 4: 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;
}
}
```

Best case O(N) worst case O(N^2)
Example 5: how to 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;
```

This example is the best reminder:
The code meaning of this example is the dichotomy search, which can be expressed by > > 1 / 2 without considering the closed and open interval. Keep looking through this dichotomy
O(N)； Either n is 1 or n is strlen (array);

There are also important recursive algorithm time complexity calculations:
1. Each function call is O (1), then it depends on its recursion times.
2. If each function call is not O (1), it depends on the accumulation of times in his recursive call.

An example of recursive factorial calculation: Fac?

```long long Fac(size_t N)
{
if(0 == N)
return 1;
return Fac(N-1)*N;
}
```

O(N);
Example 2: calculate the time complexity of Fibonacci recursive Fib?

```long long Fib(size_t N)
{
if(N < 3)
return 1;
return Fib(N-1) + Fib(N-2);
}
```

Keep one minute and two until it is 0
1+2+4+8+16...2^(N-2) = 2 ^N;
Using the formula of equal ratio sequence
2^n-1-1=2 ^N
We can get O(2^N) through recursive rules.

Topics: C Algorithm data structure