MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / insort_right

Function insort_right

searches/binary_search.py:140–177  ·  view source on GitHub ↗

Inserts a given value into a sorted array after other values with the same value. It has the same interface as https://docs.python.org/3/library/bisect.html#bisect.insort_right . :param sorted_collection: some ascending sorted collection with comparable items :param item: item

(
    sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
)

Source from the content-addressed store, hash-verified

138
139
140def insort_right(
141 sorted_collection: list[int], item: int, lo: int = 0, hi: int = -1
142) -> None:
143 """
144 Inserts a given value into a sorted array after other values with the same value.
145
146 It has the same interface as
147 https://docs.python.org/3/library/bisect.html#bisect.insort_right .
148
149 :param sorted_collection: some ascending sorted collection with comparable items
150 :param item: item to insert
151 :param lo: lowest index to consider (as in sorted_collection[lo:hi])
152 :param hi: past the highest index to consider (as in sorted_collection[lo:hi])
153
154 Examples:
155 >>> sorted_collection = [0, 5, 7, 10, 15]
156 >>> insort_right(sorted_collection, 6)
157 >>> sorted_collection
158 [0, 5, 6, 7, 10, 15]
159 >>> sorted_collection = [(0, 0), (5, 5), (7, 7), (10, 10), (15, 15)]
160 >>> item = (5, 5)
161 >>> insort_right(sorted_collection, item)
162 >>> sorted_collection
163 [(0, 0), (5, 5), (5, 5), (7, 7), (10, 10), (15, 15)]
164 >>> item is sorted_collection[1]
165 False
166 >>> item is sorted_collection[2]
167 True
168 >>> sorted_collection = [0, 5, 7, 10, 15]
169 >>> insort_right(sorted_collection, 20)
170 >>> sorted_collection
171 [0, 5, 7, 10, 15, 20]
172 >>> sorted_collection = [0, 5, 7, 10, 15]
173 >>> insort_right(sorted_collection, 15, 1, 3)
174 >>> sorted_collection
175 [0, 5, 7, 15, 10, 15]
176 """
177 sorted_collection.insert(bisect_right(sorted_collection, item, lo, hi), item)
178
179
180def binary_search(sorted_collection: list[int], item: int) -> int:

Callers

nothing calls this directly

Calls 2

bisect_rightFunction · 0.85
insertMethod · 0.45

Tested by

no test coverage detected