Sitemap
Diane Khambu

Diane Khambu

Engineer

bisect module — array bisection algorithm

3 min readApr 17, 2023

After the atmospheric rain, spring in California has been gorgeous. Hope yours spring too is great!

Press enter or click to view image in full size
Be Golden! © Diane Khambu

bisect module supports maintaining a list in sorted order without having to sort the list after each insertion.

Below are the functions provided by the module:

0) bisect.bisect_left(a, x, lo=0, hi=len(a), *, key=None)

The parameters lo and hi may be used to specify a subset of the list which should be considered; by default entire list is used.

key specifies a key function of one argument that is used to extract a comparison key from each element in the array.

a array is sorted.

The returned insertion point i partitions the array a into two halves so that all(val < x for val in a[lo: i]) for the left side and all(val >= x for val in a[i: hi]) for the right side.

  1. bisect.bisect_right(a, x, lo=0, hi=len(a), *, key=None)
  2. bisect.bisect(a, x, lo=0, hi=len(a), *, key=None)

Similar to bisect_left() , but returns an insertion point i which comes after any existing entries of x in a .

The returned insertion point i partitions the array a into two halves so that all(val <= x for val in a[lo: i]) for the left side and all(val > x for val in a[i: hi]) for the right side.

3) bisect.insort_left(a, x, lo=0, hi=len(a), *, key=None)

The function first runs bisect_left() to locate an insertion point, i. Next inserts x at the i to maintain sort order.

4) bisect.insort_right(a, x, lo=0, hi=len(a), *, key=None)

5) bisect.insort(a, x, lo=0, hi=len(a), *, key=None)

Similar to insert_right() to locate an insertion point, i . Next inserts x at the i to maintain sort order.

Get Diane Khambu’s stories in your inbox

Join Medium for free to get updates from this writer.

** key parameter is added on python 3.10.

Searching Sorted Lists:

The bisect() functions are useful for finding insertion points. The following five functions show how to transform them into a standard lookups for sorted list.

0) Finding an index

from bisect import bisect_left

def index(a, x):
'Locate the leftmost value equal to x'
i = bisect_left(a, x)
if i != len(a) and a[i] == x:
return i
raise ValueError

a = [7, 10, 54, 61, 67, 83, 85, 90, 93, 99]
idx = index(a, 85)
print(idx) # prints 6
  1. Finding less than
from bisect import bisect_left

def find_lt(a, x):
'Find right most value less than x'
i = bisect_left(a, x)
if i:
return a[i-1]
raise ValueError

a = [7, 10, 54, 61, 67, 83, 85, 90, 93, 99]
lt_ele = find_lt(a, 67)
print(lt_ele) # 61

2) Finding less than or equal

from bisect import bisect_right

def find_le(a, x):
'Find rightmost value less than or equal to x'
i = bisect_right(a, x)
if i:
return a[i-1]
raise ValueError
a = [7, 10, 54, 61, 67, 83, 85, 90, 93, 99]
lte_ele = find_le(a, 68)
print(lte_ele) # prints 67

3) Finding greater than

from bisect import bisect_right

def find_gt(a, x):
'Find leftmost value greater than x'
i = bisect_right(a, x)
if i != len(a):
return a[i]
raise ValueError

a = [7, 10, 54, 61, 67, 83, 85, 90, 93, 99]
gt_ele = find_gt(a, 93)
print(gt_ele) # prints 99

4) Finding greater than or equals to

from bisect import bisect_left

def find_gte(a, x):
'Find the leftmost value greater than or equal to x'
i = bisect_left(a, x)
if i != len(a):
return a[i]
raise ValueError
a = [7, 10, 54, 61, 67, 83, 85, 90, 93, 99]
gte_ele = find_gt(a, 90)
print(gte_ele) # prints 90

More examples of the application of the algorithm:

Let’s have a movie review meter:

import bisect

def tomatometer(rating: int, breakpoints: List[int]: [60, 70, 80, 90],\
stars: List[str]: ['*', '**', '***', '****', '*****']):
i = bisect.bisect(breakpoints, rating)
return stars[i]

print(tomatometer(69)) # prints '**'
print(tomatometer(70)) # prints '***'

In conclusion, python has a bisect module that allows us to insert to an ordered collection or search index of a value.

Congratulations on the completion! 🎈 Thank you for reading.

See you on my next post. Until then ✨.

Inspiration:

You can support me on Patreon!

--

--