Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 663 Bytes

File metadata and controls

25 lines (21 loc) · 663 Bytes

Search - Binary Search

The idea of binary search is to use the information that the array is sorted.
Time complexity is O(log n)
Typical implementation:

def binary_search(array, search_item):
    left = 0
    right = len(array) - 1
    
    while left <= right:
        mid = (left + right) // 2
    
        if array[mid] < search_item: 
            left = mid + 1
        elif array[mid] == search_item:
            return mid
        else:
            right = mid - 1

Related Problems

HackerRank - Minimum Time Required

Related Resources

https://www.geeksforgeeks.org/binary-search/