Search

Big O and Recursion

카테고리
Algorithm and DataStructure
Index
Big O and Recursion
날짜
2025/01/17

1. Big O

1.1 Big O

Big O is a notation used to describe the computational complexity of an algorithm
There are 2 types of computational complexity we have to consider : time complexity & space complexity
Time complexity : amount of time the algorithm needs to run relative to the input size
→ How much longer does the algorithm take to complete as the input size grows?
Space complexity : amount of memory allocated by the algorithm when running relative to the input size
→ How much more memory does the algorithm use as the input size grows?
Time complexity is considered more important than space complexity

1.2 How complexity works

a math formula describes complexity, nn is often used to denote the length of an input array or string.
O(n),O(n2),O(nlogn),O(nm)O(n), O(n^2), O(nlogn), O(n\cdot m)
mm is used to denote any nother arbitrary variables
time complexity describes how the number of operations changes as the input change

Calculating Time Complexity

1.
ignore constants
→ as the input size increases, the number of operation required increase linearly
→ O(9999999n) = O(8n) = O(n)
2.
consider the complexity as the variables tend to infinity
O(2n+n2500n)=O(2n)O(2^n + n^2 - 500n) = O(2^n) , 2n 2^n plays the most crucial role when nn \rightarrow \infty
3.
Best complexity possible is O(1)O(1), constant time/ constant space
→ algorithm always used the same amount of resources, regardless of the input
→ it doesn’t mean algorithm is fast, it means that its runtime is independent of the input size

Logarithmic time

logarithm is the inverse operation to exponents
logarithmic time complexity O(logn)O(logn) means the input is being reduced by a percentatge at every step
→ Binary search algorithm runs in O(logn)O(logn), at first step, we consider the entire input nn, and at second step, we only consider n/2n/2 elements, after second step, we consider n/4n/4 elements and so on. As we reduce our search space by 1/2, we can run in logarthmic time complexity.