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
n elements and a
target value, write a function to search target in
target exists, then return its index, otherwise return
Divide and conquer algorithm. A binary search is a searching algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item until you’ve narrowed down the possible locations to just one. As compared to linear, binary search is much faster with Time Complexity of O(logN) whereas the linear search algorithm works in O(N) time complexity. The implementation of binary search can be done iteratively or recursively.