The complexity of the front-end algorithm

algorithm

Algorithm overview

  1. the complexity
  2. double pointer
  3. sliding window
  4. Binary tree

the complexity

Less time, less space

O(n) algorithm execution time scale

time complexity

Time complexity is a concept in computer science used to measure an algorithm执行时间随输入规模变化的程度 .

For the same algorithm, the running time increases linearly as the amount of input data increases, so its time complexity is O(n) , where n represents the amount of input data.

For example,

  • The time complexity of insertion sort is O(n^2) because for a list of n elements, it requires n*(n-1)/2 comparisons and exchanges.

  • The time complexity of quick sort is O(nlogn), because on average it only needs to split the list once, then sort the two sublists separately, and finally merge them once.

Time complexity can be used to evaluate the efficiency of an algorithm, especially when dealing with large-scale data.

When designing and selecting algorithms, you should try to choose algorithms with lower time complexity to improve the running efficiency of the program.

function a(){
    
    
  console.log('hello')
  return 1
} // 总共执行 2次

function b(){
    
    
  for(let i = 0 ; i < n ;i++){
    
     // n+1 次
    console.log('hello') // n 次
  }
  return 1 // 1 次
} // 总共执行 2n+2次

function c(){
    
     
  let sum = 0;  // 1 次
  let i = 1;  // 1 次
  let j = 1;  // 1 次

  for( ; i < n ;i++){
    
      // n 次
    j = 1 // 1 * n 次
    for( ; j < n ;j++){
    
      // n * n 次
      sum = sum + i + j // n * n 次
    }
  }
} // 总共执行 2 (n^2) + 2n + 3 次
Time complexity summary

  1. The part with the largest time complexity metric has the greatest impact
  2. Loops, nesting, and recursion have multi-dimensional complexity concepts

O(1) --> O(logN) --> O(N) --> O(NlogN) --> O(N^2)–> O(N^3)

// 二分查找算法 --  O(logN)
let i = 1
while(i <= n ){
    
    
  i = i * 2
} 
// log2^n 可称为 logN

// ----------------------// 
function a (n){
    
    
  let i = 1
  while(i <= n ){
    
    
    i = i * 2
  }
  return i
}

function cal(n){
    
    
  let sum = 0
  for(let i = 0 ; i < n ;i++){
    
    
    sum ++ a(n)
  }

  return sum
}
multidimensional complexity

Multidimensional complexity usually refers to the complexity in multiple dimensions, which is used to measure the complexity of a system or model in multiple dimensions.

In the fields of computer science and artificial intelligence, multidimensional complexity is often used to evaluate an algorithm's efficiency, computational time, space requirements, etc.

It can help us better understand the complexity and performance of algorithms in order to optimize and improve them.

Multidimensional complexity can include time complexity, space complexity, algorithm complexity, etc.

Among them, time complexity refers to the complexity of the algorithm execution time, space complexity refers to the size of the storage space required by the algorithm, and algorithm complexity refers to the complexity of the algorithm itself.

When calculating multidimensional complexity, it is necessary to consider the performance of an algorithm in different dimensions in order to better evaluate its efficiency and feasibility.

This helps us make better decisions when designing and implementing algorithms, and improves algorithm efficiency and performance.

Time complexity breakdown
  1. Best case time complexity
  2. Worst case time complexity
  3. Average case time complexity
  4. Amortized time complexity

Example

  • 12345 O(1)
  • 3n+4 O(n)
  • 3n^2 + 4n + 5 O(n^2)
  • 3log2^n + 4 logn
  • 2^n O(2^n)
Time complexity example

Here are some examples of time complexity:

O(1) constant time complexity:

For example, when searching for an element in an array, regardless of the size of the array, the search operation time is always a constant. This is the time complexity of O(1).

Similar operations include accessing an element in an array, inserting or deleting a node in a linked list , etc.

O(log n) logarithmic time complexity:

For example, the binary search algorithm can reduce the search range by half for each comparison, so its time complexity is O(log n).

O(n) linear time complexity:

For example, to traverse all elements of an array or list , the time is proportional to the size n of the array or list, and its time complexity is O(n).

Similar algorithms include linear search, simple sorting, etc.

O(n log n) linear logarithmic time complexity:

For example, the time complexity of algorithms such as quick sort and merge sort is O(n log n).

These algorithms are more efficient when processing large-scale data.

O(n^2) square time complexity:

For example, the time complexity of algorithms such as bubble sort and insertion sort is O(n^2).

These algorithms are more efficient when processing small-scale data, but may be slower when processing large-scale data.

O(n^3) cubic time complexity:

For example, the time complexity of operations such as matrix multiplication is O(n^3).

This time complexity is usually inefficient when dealing with large-scale data.

These time complexities can be used to evaluate the efficiency of different algorithms and select the appropriate algorithm based on the scale and characteristics of the actual problem.

For more details, please search " " on WeChat前端爱好者 and click me to view .

space complexity

Space Complexity is a measure of the amount of storage space an algorithm temporarily occupies during operation, recorded as S(n)=O(f(n)). It is also a function of problem size n .

The space complexity of an algorithm is generally given in terms of orders of magnitude.

In addition to time complexity, space complexity is also an important consideration when evaluating an algorithm.

An efficient algorithm should have good time complexity and small space complexity .

The analysis of space complexity can help us understand the maximum storage space required by the algorithm during operation, thereby providing guidance for the implementation and optimization of the algorithm.

When designing and analyzing algorithms, the space complexity of the algorithm should be reduced as much as possible to avoid excessive memory usage and resource waste.

It should be noted that the space complexity only considers the storage space temporarily occupied during the running of the algorithm, and does not include the storage space occupied by the algorithm itself.

If you need to fully evaluate the efficiency of an algorithm, you also need to consider the time complexity and other performance indicators of the algorithm.

inction a(n) {
    
    
  const arr = []; 
  arr.length = n; // 开辟了空间
  for(let i = 0; i < n; i++) {
    
    
    arr[i] = i *i
  }
}

Trend : O(1) --> O(logN) --> O(N) --> O(NlogN) --> O(N^2)–> O(N^3)

Guess you like

Origin blog.csdn.net/BradenHan/article/details/135258433