Complexities like O(1) and O(n) are simple to understand. O(1) means it requires constant time to perform operations like to reach an element in constant time as in case of dictionary and O(n) means, it depends on the value of n to perform operations such as searching an element in an array of n elements.
But for O(Log n), it is not that simple. Let us discuss this with the help of Binary Search Algorithm whose complexity is O(log n).
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
Sorted Array of 10 elements: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91 Let us say we want to search for 23.
Finding the given element:
Now to find 23, there will be many iterations with each having steps as mentioned in the figure above:
Array: 2, 5, 8, 12, 16, 23, 38, 56, 72, 91
- Select the middle element. (here 16)
- Since 23 is greater than 16, so we divide the array into two halves and consider the sub-array after element 16.
- Now this subarray with the elements after 16 will be taken into next iteration.
Array: 23, 38, 56, 72, 91
- Select the middle element. (now 56)
- Since 23 is smaller than 56, so we divide the array into two halves and consider the sub-array before element 56.
- Now this subarray with the elements before 56 will be taken into next iteration.
Array: 23, 38
- Select the middle element. (now 23)
- Since 23 is the middle element. So the iterations will now stop.
- Let say the iteration in Binary Search terminates after k iterations. In the above example, it terminates after 3 iterations, so here k = 3
- At each iteration, the array is divided by half. So let’s say the length of array at any iteration is n
- At Iteration 1,
Length of array = n
- At Iteration 2,
Length of array = n⁄2
- At Iteration 3,
Length of array = (n⁄2)⁄2 = n⁄22
- Therefore, after Iteration k,
Length of array = n⁄2k
- Also, we know that after
After k divisions, the length of array becomes 1
Length of array = n⁄2k = 1 => n = 2k
- Applying log function on both sides:
=> log2 (n) = log2 (2k) => log2 (n) = k log2 (2)
- As (loga (a) = 1)
=> k = log2 (n)
- Complexity analysis of various operations of Binary Min Heap
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Practice Questions on Time Complexity Analysis
- Time Complexity Analysis | Tower Of Hanoi (Recursion)
- Analysis of Algorithms | Set 1 (Asymptotic Analysis)
- Analysis of Algorithms | Set 4 (Analysis of Loops)
- Analysis of Algorithm | Set 5 (Amortized Analysis Introduction)
- Analysis of Algorithms | Big-O analysis
- Meta Binary Search | One-Sided Binary Search
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- What does 'Space Complexity' mean?
- Time Complexity of building a heap
- An interesting time complexity question
- Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- Sieve of Eratosthenes in 0(n) time complexity
- Understanding Time Complexity with Simple Examples
- Time complexity of recursive Fibonacci program
Calculating Time complexity:
Hence, the time complexity of Binary Search is
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ddg2112