Algorithms: Binary Search Flashcards
Which is more efficient: linear search or binary search?
binary search is more efficient as we are able to leverage a divide and conquer strategy. We compare the target element to the middle element of the array. If the target element is less than the middle element, then we are able to ignore the right of the array. Repeating this same strategy until we find out target element or determine that the element is not present.
What is the bisect module?
Bisect is a built-in binary search module in Python that takes in a sorted array of integers and a key, and returns the index of the first occurrence of that key from the array
What does bisect_left do?
bisect_left is a method of the bisect module which returns the index of the left-most occurrence of the key
What does bisect_right do?
bisect_right is a method of the bisect module which returns the index to the right of the key element. This is used to serve as an insertion point.
What does the bisect function do?
bisect is equivalent in functionality to bisect_right
What is the purpose of insort_left and insort_right?
the purpose is to maintain the sorting of a list when inserting a new element. These both operate similarly to bisect_left and bisect_right, respectively. The difference in functionality is that insort INSERTS an element into the list whereas bisect returns the index of a target element
What is a cyclically shifted array?
an array that is able to become sorted by shifting its elements cyclically