Bisect Algorithm Functions in Python (original) (raw)

The bisect module is used to locate positions in a sorted list and insert elements while maintaining order. It works using binary search and helps determine where a value fits within an existing sorted sequence.

The bisect module mainly offers two types of functionalities, as listed below:

Finding Insertion Points

These functions return the index at which the new element should be inserted to maintain the list's sorted order.

**1. bisect.bisect(): Returns the rightmost insertion point for the element. If the element already exists, the insertion point will be after the existing entries.

bisect.bisect(list, num, beg=0, end=len(list))

**Parameters:

**Example: In this example, we find the rightmost position where the value 4 should be inserted in a sorted list.

Python `

import bisect li = [1, 3, 4, 4, 6]

print(bisect.bisect(li, 4)) print(bisect.bisect(li, 4, 0, 3))

`

**Explanation:

**2. bisect.bisect_left(): Returns the leftmost insertion point for the element. If the element exists, the insertion point will be before the existing entries.

bisect.bisect_left(list, num, beg=0, end=len(list))

**Example: Here, we find the leftmost position where the value should be inserted.

Python `

import bisect li = [1, 3, 4, 4, 6]

print(bisect.bisect_left(li, 4)) print(bisect.bisect_left(li, 5))

`

**Explanation:

**3. bisect.bisect_right(): Identical to bisect.bisect(), returns the rightmost insertion point.

bisect.bisect_right(list, num, beg=0, end=len(list))

**Example: In this code, we find the rightmost insertion position similar to bisect().

Python `

import bisect li = [1, 3, 4, 4, 6]

print(bisect.bisect_right(li, 4)) print(bisect.bisect_right(li, 4, 0, 4))

`

**Explanation:

Inserting Elements

These functions insert the element at the proper position to maintain sorting.

**1. bisect.insort(): Inserts the element at the rightmost position. Unlike bisect() functions, this actually modifies the list by inserting the element.

bisect.insort(list, num, beg=0, end=len(list))

**Example: Here, we insert a new value into the list while keeping it sorted.

Python `

import bisect

li = [1, 3, 4, 6] bisect.insort(li, 5) print(li)

`

**Explanation: insort(li, 5) inserts 5 at correct sorted position

**2. bisect.insort_left(): Inserts the element at the leftmost position.

bisect.insort_left(list, num, beg=0, end=len(list))

**Example: In this example, we insert a value at the leftmost valid position.

Python `

import bisect

li = [1, 3, 4, 4, 6] bisect.insort_left(li, 4) print(li)

`

**Explanation: insort_left(li, 4) inserts 4 before existing 4s

**3. bisect.insort_right(): Inserts the element at the rightmost position (similar to insort()).

bisect.insort_right(list, num, beg=0, end=len(list))

**Example: Here, we insert a value at the rightmost position within a given range.

Python `

import bisect

li = [1, 3, 4, 4, 6] bisect.insort_right(li, 5, 0, 4) print(li)

`

**Explanation:

Why do we need Bisect Module?

  1. Useful for binary search operations to search in a sorted list and to locate insertion points.
  2. Provides efficient methods to insert elements into a sorted list while maintaining order.
  3. Avoids the need for manual sorting after each insertion, saving time and effort.
  4. Offers functions like bisect(), bisect_left(), bisect_right() and insort() for clean optimized code.
  5. Ideal for tasks like maintaining leaderboards, ranked data or any scenario involving sorted data insertion/search.