Big O notation is used to describe the time or space complexity of an algorithm in terms of input size. Here are the common types of Big O complexities, ordered from best (fastest) to worst (slowest) in terms of performance:
Constant Time – O(1)
- Description: The algorithm takes the same amount of time regardless of input size.
- Example: Accessing an element in an array by index.
🔹 Logarithmic Time – O(log n)
- Description: The time grows logarithmically as input size increases.
- Example: Binary search in a sorted array.
🔹 Linear Time – O(n)
- Description: Time increases linearly with input size.
- Example: Looping through an array.
🔹 Linearithmic Time – O(n log n)
- Description: A combination of linear and logarithmic growth.
- Example: Efficient sorting algorithms like Merge Sort, Quick Sort (average case).
🔹 Quadratic Time – O(n²)
- Description: Time increases with the square of the input size.
- Example: Nested loops over the same data, like Bubble Sort.
🔹 Cubic Time – O(n³)
- Description: Time increases with the cube of the input size.
- Example: Triple nested loops, such as in some matrix operations.
🔹 Exponential Time – O(2ⁿ)
- Description: Time doubles with each additional input element.
- Example: Solving the Traveling Salesman Problem using brute force.
🔹 Factorial Time – O(n!)
- Description: Time grows factorially with input size.
- Example: Generating all permutations of a set.
