The Wayback Machine - https://web.archive.org/web/20250303230305/https://www.geeksforgeeks.org/bisect-algorithm-functions-in-python/
Open In App

Bisect Algorithm Functions in Python

Last Updated : 07 Dec, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The purpose of Bisect algorithm is to find a position in list where an element needs to be inserted to keep the list sorted.

Python in its definition provides the bisect algorithms using the module “bisect” which allows keeping the list in sorted order after the insertion of each element. This is essential as this reduces overhead time required to sort the list again and again after the insertion of each element. 

Important Bisection Functions 

1. bisect(list, num, beg, end) :- This function returns the position in the sorted list, where the number passed in argument can be placed so as to maintain the resultant list in sorted order. If the element is already present in the list, the rightmost position where element has to be inserted is returned.

This function takes 4 arguments, list which has to be worked with, a number to insert, starting position in list to consider, ending position which has to be considered. 

2. bisect_left(list, num, beg, end) :- This function returns the position in the sorted list, where the number passed in argument can be placed so as to maintain the resultant list in sorted order. If the element is already present in the list, the leftmost position where element has to be inserted is returned. 

This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered. 

3. bisect_right(list, num, beg, end) :- This function works similar to the “bisect()” and mentioned above. 

Python3




# Python code to demonstrate the working of
# bisect(), bisect_left() and bisect_right()
 
# importing "bisect" for bisection operations
import bisect
 
# initializing list
li = [1, 3, 4, 4, 4, 6, 7]
 
# using bisect() to find index to insert new element
# returns 5 ( right most possible index )
print ("Rightmost index to insert, so list remains sorted is : ",
       end="")
print (bisect.bisect(li, 4))
 
# using bisect_left() to find index to insert new element
# returns 2 ( left most possible index )
print ("Leftmost index to insert, so list remains sorted is : ",
       end="")
print (bisect.bisect_left(li, 4))
 
# using bisect_right() to find index to insert new element
# returns 4 ( right most possible index )
print ("Rightmost index to insert, so list remains sorted is : ",
       end="")
print (bisect.bisect_right(li, 4, 0, 4))


Output

Rightmost index to insert, so list remains sorted is : 5
Leftmost index to insert, so list remains sorted is : 2
Rightmost index to insert, so list remains sorted is : 4

Time Complexity: O(log(n)), Bisect method works on the concept of binary search
Auxiliary Space:  O(1)

4. insort(list, num, beg, end) :- This function returns the sorted list after inserting number in appropriate position, if the element is already present in the list, the element is inserted at the rightmost possible position. 

This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered. 

5. insort_left(list, num, beg, end) :- This function returns the sorted list after inserting number in appropriate position, if the element is already present in the list, the element is inserted at the leftmost possible position. 

This function takes 4 arguments, list which has to be worked with, number to insert, starting position in list to consider, ending position which has to be considered. 

6. insort_right(list, num, beg, end) :- This function works similar to the “insort()” as mentioned above. 

Python3




# Python code to demonstrate the working of
# insort(), insort_left() and insort_right()
 
# importing "bisect" for bisection operations
import bisect
 
# initializing list
li1 = [1, 3, 4, 4, 4, 6, 7]
 
# initializing list
li2 = [1, 3, 4, 4, 4, 6, 7]
 
# initializing list
li3 = [1, 3, 4, 4, 4, 6, 7]
 
# using insort() to insert 5 at appropriate position
# inserts at 6th position
bisect.insort(li1, 5)
 
print ("The list after inserting new element using insort() is : ")
for i in range(0, 7):
    print(li1[i], end=" ")
 
# using insort_left() to insert 5 at appropriate position
# inserts at 6th position
bisect.insort_left(li2, 5)
 
print("\r")
 
print ("The list after inserting new element using insort_left() is : ")
for i in range(0, 7):
    print(li2[i], end=" ")
 
print("\r")
 
# using insort_right() to insert 5 at appropriate position
# inserts at 5th position
bisect.insort_right(li3, 5, 0, 4)
 
print ("The list after inserting new element using insort_right() is : ")
for i in range(0, 7):
    print(li3[i], end=" ")


Output

The list after inserting new element using insort() is : 
1 3 4 4 4 5 6 
The list after inserting new element using insort_left() is : 
1 3 4 4 4 5 6 
The list after inserting new element using insort_right() is : 
1 3 4 4 5 4 6 

Time Complexity: O(n)
Auxiliary Space:  O(1)


Level up your coding with DSA Python in 90 days! Master key algorithms, solve complex problems, and prepare for top tech interviews. Join the Three 90 Challenge—complete 90% of the course in 90 days and earn a 90% refund. Start your Python DSA journey today!


Next Article
Practice Tags :

Similar Reads

Binary Search (bisect) in Python
Binary Search is a technique used to search element in a sorted list. In this article, we will looking at library functions to do Binary Search.Finding first occurrence of an element. bisect.bisect_left(a, x, lo=0, hi=len(a)) : Returns leftmost insertion point of x in a sorted list. Last two parameters are optional, they are used to search in sub
2 min read
Mathematical Functions in Python | Set 1 (Numeric Functions)
In python a number of mathematical operations can be performed with ease by importing a module named "math" which defines various functions which makes our tasks easier. 1. ceil() :- This function returns the smallest integral value greater than the number. If number is already integer, same number is returned. 2. floor() :- This function returns t
3 min read
Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
Numeric functions are discussed in set 1 below Mathematical Functions in Python | Set 1 ( Numeric Functions) Logarithmic and power functions are discussed in this set. 1. exp(a) :- This function returns the value of e raised to the power a (e**a) . 2. log(a, b) :- This function returns the logarithmic value of a with base b. If base is not mentione
3 min read
Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
Some of the mathematical functions are discussed in below set 1 and set 2 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Trigonometric and angular functions are discussed in this article. 1. sin() :- This function returns the sine of value passed as argument. T
3 min read
Mathematical Functions in Python | Set 4 (Special Functions and Constants)
Some of the mathematical functions are discussed in below set 1, set 2 and set 3 Mathematical Functions in Python | Set 1 (Numeric Functions) Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions) Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions) Special Functions and constants are discussed in this
2 min read
Partial Functions in Python
Partial functions allow us to fix a certain number of arguments of a function and generate a new function. In this article, we will try to understand the concept of Partial Functions with different examples in Python.What are Partial functions and the use of partial functions in Python?Partial functions in Python is a function that is created by fi
5 min read
Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
Python has defined a module, "time" which allows us to handle various operations regarding time, its conversions and representations, which find its use in various applications in life. The beginning of time started measuring from 1 January, 12:00 am, 1970 and this very time is termed as "epoch" in Python. Operations on Time in Python Python time.t
4 min read
Time Functions in Python | Set-2 (Date Manipulations)
Some of Time Functions are discussed in Set 1 Date manipulation can also be performed using Python using "datetime" module and using "date" class in it. Operations on Date : 1. MINYEAR :- It displays the minimum year that can be represented using date class. 2. MAXYEAR :- It displays the maximum year that can be represented using date class. Python
2 min read
Calendar Functions in Python | Set 1( calendar(), month(), isleap()...)
Python defines an inbuilt module "calendar" which handles operations related to the calendar.Operations on the calendar : 1. calendar(year, w, l, c):- This function displays the year, the width of characters, no. of lines per week, and column separations.2. firstweekday() :- This function returns the first week day number. By default 0 (Monday). P
5 min read
Calendar Functions in Python | Set 2(monthrange(), prcal(), weekday()...)
Some of calendar functions are discussed in the Set 1 1. monthrange(year, month) :- This function returns two integers, first, the starting day number of week(0 as monday) , second, the number of days in the month. 2. prcal(year, w, l, c) :- This function also prints the calendar of specific year but there is no need of "print" operation to execu
5 min read