Examples of Algorithms which has O1, On log n and Olog n complexities
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding Algorithmic Complexity with Examples
When evaluating the efficiency of an algorithm, Big O notation is an essential mathematical tool used by computer scientists to express time complexity relative to the input size. This article delves into three common algorithmic complexities: , , and . We will explore examples, provide technical explanations, and present a concise comparison table.
O(1) Complexity: Constant Time
Description:
An algorithm performs its operations in constant time, irrespective of the size of the input. These algorithms are extremely efficient since they don't scale with input size.
Example:
Accessing an element in an array by its index is a classic example of .
In the above function, retrieving the index element from arr requires a single operation, hence the complexity is .
Use Cases:
- Hash table lookups
- Array indexing
- Simple arithmetic operations
O(n \log n) Complexity: Linearithmic Time
Description:
An algorithm typically involves a divide-and-conquer approach. The input is divided into smaller parts, processed independently, and combined. These algorithms are common in efficient sorting and searching routines.
Example:
Merge Sort is a famous sorting algorithm with complexity. It splits the input array into halves recursively until arrays are trivially sort-able, then merges them back together.
Use Cases:
- Sorting algorithms e.g., Quick Sort, Merge Sort
- Some search and merge operations in databases
O(\log n) Complexity: Logarithmic Time
Description:
Algorithms with complexity are exceptionally efficient for operations that repeatedly halve the input size until reaching a base state, common in binary search scenarios.
Example:
Binary Search is a widely known algorithm that effectively finds an element's index in a sorted array.
Use Cases:
- Searching in a balanced binary search tree
- Looking up an entry in a logarithmic-time data structure
Comparative Analysis
Here's a table summarizing the complexities discussed:
| Time Complexity | Notation | Example Algorithms/Operations | Efficiency Impact |
| Constant | Array indexing, hash table operations | Most efficient, fixed time | |
| Logarithmic | Binary Search, balanced trees (AVL, Red-Black) | Efficient for large data, reduces problem size exponentially | |
| Linearithmic | Merge Sort, Quick Sort | Faster than but larger overhead than |
Conclusion
Understanding these complexities is crucial for selecting the right algorithm for a task, especially when dealing with large-scale data. While offers the most constant execution, and provide efficient solutions for searching and sorting, essential in many modern applications.

