ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Big O]
    개발/자료구조 2022. 6. 20. 00:24

    Big O 

    빅오 표기법이란 입력된 값에 의해 시간 복잡도와 공간복잡도를 측정 할수 있는 수학전 표현 방식이다. 

    Constant time : O (1) 

    function add (num1, num2) {
       return num1 + num2; 
    }

    For the above function, there will always be two inputs: num1 and num2. The only operation that will ever be done on them is simple addition. This means that no matter what the input, it will always take a very small amount of time to complete the function and the amount of time does not vary depending on input size. 

     

    Linear time : O(n) 

    function loopThroughArray(array) {
               for (let i = 0; i < array.length; i++) {
                     console.log(array[i]); 
               }
    };

    This is where linear time complexity comes into play. The amount of elements in the array (n elements) directly influences the time it will take to calculate the output.  

     

    Quadratic: O(n^2)

    function nestedArrayLoop(arr1,v ) {
        for (let i = 0; i < array.length; i++) {
                for (let j = 0; j < array[i].length; j++) {
                      console.log(array[i][j];
                 }
          }
    }

    For nested arrays, we’d need to loop through both the outer array and each inner array. The amount of time it takes the loop through is multiplied by the number of elements contained in each array. If we take n * n elements we can also state this as n². The amount of input affects the output time exponentially.

     

    Logarithmic: O(log n) 

    (한번 처리할때 데이터의 양이 절반씩 떨어지는 함수) 

    The real power of this is when we get to very large numbers. If I use divide and conquer, I could find any item in that array in few steps. 

     

    '개발 > 자료구조' 카테고리의 다른 글

    [Hash Table]  (0) 2022.10.05
Designed by Tistory.