Explained With a Problem From LeetCode
For today’s algorithm, I picked the binary search which is one of the most commonly asked topics in technical interviews. The problem named binary search from LeetCode will be used to explain this concept:
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
Input: nums = [-1,0,3,5,9,12], target = 9
Explanation: 9 exists in nums and its index is 4
While linear searching checks every single element one at a time and works in a sorted or unsorted piece of data, binary searching eliminates half of the remaining elements at a time and works only on sorted data. The basic idea behind this technique is to find a certain element in a sorted sequence using a divide and conquer approach. This algorithm allows us to quickly find the position of a target value within a sorted list basically by reducing the search area by half at each iteration and it is usually faster than linear search for large amounts of data. We will use iterative binary search technique to solve the given question and here is a visual representation of our approach:
We will first list the steps that will be taken to solve this question:
- Set two pointers; the first one will point to the index of the first element in a given array (min = 0) and the second one will point to the index of the last element in the array (max = nums.length — 1).
- While min <= max
- Calculate the middle number (mid) by taking an average of min and max and use Math.floor to round it down.
- Compare the middle element of the array (nums[mid]) to the target value (target):
- If they are equal, return the index of the middle number; mid.
- If it is smaller than target, set min = mid + 1, and then continue the search on the right.
- Else, set max = mid — 1, and then continue the search on the left.
5. Repeat steps 3 and 4 until the condition is met or the loop finishes.
6. If the target is not present in the array, return -1.
As you see in the above code, we divide the array in half and check whether or not the target value is greater than, less than, or equal to the middle element of the array. We move and only search the left half of the array if the target value is smaller than the middle value or move to the right side of the array if the target value is greater than the middle value. This process will continue until the target value is found or the starting point exceeds the ending point (min ≤ max).
We know that the time complexity of the linear search is O(n), where n is the size of the given array. Binary search is a great algorithm to learn because it significantly improves the time complexity especially when we are dealing with large data sets. Since the array is already sorted, it allows us to search in time complexity of O(log n) and it gives us O(1) space complexity. For example; if the size of the given array is 8, binary search will need 3 steps with the worst-case scenario because the logarithm of 8 to base 2 is equal to 3.
Thank you for taking the time to read my article and I hope it was helpful and you got the basic idea of how this fundamental searching algorithm works. Below are some of my articles written about other commonly asked interview challenges, feel free to take a look if you find them interesting: