Skip to main content

Binary Search

bisect

  • import bisect
# Returns the left most insertion index to maintain the sorted list
leftIdx = bisect.bisect_left(arr, item, lo=0, hi=len(arr))

# Returns the right most insertion index to maintain the sorted list
rightIdx = bisect.bisect_right(arr, item, lo=0, hi=len(arr))

# Same as bisect.bisect_right
rightIdx = bisect.bisect(arr, item, lo=0, hi=len(arr))

# Insert the item at the left most insertion point in the original array
bisect.insort_left(arr, item, lo=0, hi=len(arr))

# Insert the item at the right most insertion point in the original array
bisect.insort_right(arr, item, lo=0, hi=len(arr))

# Same as bisect.insort_right
bisect.insort(arr, item, lo=0, hi=len(arr))