Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between insort_left and insort_right in bisect?

Tags:

python

Why do both insort_left and insort_right exist; isn't it always the same result since the elements are equal?

>>> import bisect
>>> foo = [1,2,3]
>>> 
>>> bisect.insort_left(foo, 1)
>>> foo
[1, 1, 2, 3]
>>> 
>>> bisect.insort_right(foo, 1)
>>> foo
[1, 1, 1, 2, 3]
like image 809
myartsev Avatar asked Jul 13 '21 22:07

myartsev


People also ask

What does bisect Bisect_left do?

bisect. bisect_left returns the leftmost place in the sorted list to insert the given element. bisect. bisect_right returns the rightmost place in the sorted list to insert the given element.

What is bisect Insort?

The bisect module in Python assists in preserving a list in a sorted order, as it bypasses the sort operation after each insertion. Insort is one of the functions of the bisect module.

How does Python bisect work?

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 to keep the list in sorted order after insertion of each element.

What is Insort?

The insort() method inserts a new element into an already sorted Python list. If the list already has existing elements as the new element then the new element is inserted into the right of the last such existing element. The functions insort() and insort_right() behave the same way.

What is the difference between bisect left and bisect right?

bisect_left would insert elements to the left of identical elements. bisect_right would insert elements to the right of identical elements. There are two things to be understood: bisect.bisect and bisect.bisect_right work the same way. These return the rightmost position where the element can be inserted without breaking the order of elements.

What is the use of bisect in Python?

Bisect Algorithm Functions in Python. 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 to keep the list in sorted order after insertion of each element.

Why does bisect_left return index 0?

I love the answer conceptually, but it is nonetheless unclear. bisect_left returns index 0, because that's the largest possible insert index where the inserted element is truly smaller. Smaller than what? The only numbers that appear in the example are 0 (so no number in the problem is smaller than any other). It would help to fill that in! Thanks!

What is the rightmost index to insert so list remains sorted?

The rightmost index to insert, so list remains sorted is : 5 The leftmost index to insert, so list remains sorted is : 2 The rightmost index to insert, so list remains sorted is : 4 O (log (n)) -> Bisect method works on the concept of binary search


Video Answer


2 Answers

For most purposes the results are indistinguishable, but there are cases where it can matter, particularly when using the optional key= argument.

Do you understand the difference between sorting algorithms that are, or aren't, guaranteed to be "stable"? If not, click the link ;-)

ys = []
for x in xs:
    bisect.insort_right(ys, x)

fills ys with a stable sort of xs entries, but using insort_left() instead would not.

like image 171
Tim Peters Avatar answered Nov 14 '22 22:11

Tim Peters


Objects can be equivalent without being identical.

>>> bisect.insort_left(foo, 1.0)
>>> foo
[1.0, 1, 1, 1, 2, 3]
>>> 
>>> bisect.insort_right(foo, 1.0)
>>> foo
[1.0, 1, 1, 1, 1.0, 2, 3]
like image 44
wjandrea Avatar answered Nov 14 '22 22:11

wjandrea