Bisect Algorithm Functions in Python
Last Updated :
07 Dec, 2022
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
import bisect
li = [ 1 , 3 , 4 , 4 , 4 , 6 , 7 ]
print ( "Rightmost index to insert, so list remains sorted is : " ,
end = "")
print (bisect.bisect(li, 4 ))
print ( "Leftmost index to insert, so list remains sorted is : " ,
end = "")
print (bisect.bisect_left(li, 4 ))
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
import bisect
li1 = [ 1 , 3 , 4 , 4 , 4 , 6 , 7 ]
li2 = [ 1 , 3 , 4 , 4 , 4 , 6 , 7 ]
li3 = [ 1 , 3 , 4 , 4 , 4 , 6 , 7 ]
bisect.insort(li1, 5 )
print ( "The list after inserting new element using insort() is : " )
for i in range ( 0 , 7 ):
print (li1[i], end = " " )
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" )
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)
Please Login to comment...