I have completed an iterative implementation of Binary Search in python and was wondering if there is an efficient way to ensure that your input is always sorted before sorting it if needed. How can one validate the input to be sorted? What is the time complexity if the input is not sorted? I would also appreciate comments if this code can be made more efficient.
def binary_search(array, search):
low = 0
high = len(array)
while low <= high and len(array[low:high]):
mid = (low + high) // 2
if array[mid] == search:
return "Found " + str(search) + " at index " + str(mid)
elif search > array[mid]:
low = mid + 1
elif search < array[mid]:
high = mid - 1
return str(search) + " not found."
search_through = [2, 3, 4, 10, 40]
to_search = 49
print(binary_search(search_through, to_search))
Output: 49 not found.
There are two scenarios to consider. Either:
or:
So in either case, it makes no sense to verify that the list is sorted. Doing that is a waste of time, as then you might as well use that iteration to look for the value and forget about the binary search all together. A binary search is only useful when it is guaranteed that the input is sorted. There should be no need to verify that -- it should be trusted that it is the case.
A verification step followed by a binary search would deteriorate the whole efficiency gain that a pure binary search delivers: the O(log𝑛) complexity of the binary search would be lost in the O(𝑛) complexity of the verification part, giving O(𝑛) as overall time complexity.