0

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.

22
  • 1
    The and len(array[low:high]) should not be necessary, and once the code works correctly, it won't be necessary. Commented Aug 11, 2022 at 4:55
  • 2
    1. You ask: "What is the time complexity if the input is not sorted?"--binary search requires the list to be sorted to work. 2. Check out Pythonic way to check if a list is sorted or not for myriad ways to see if a list is sorted. 3. The third conditional can be changed to an else i.e. elif search < array[mid]: can become else: because of the earlier conditionals. Commented Aug 11, 2022 at 7:52
  • 1
    @DarrylG: O(Log(N)) on a sorted array is familiar, found in any textbook. But the question is about an unsorted array. Though trivial, this is never discussed. You might very well a priori imagine that the algorithm does not terminate, or degenerates to linear complexity. Commented Aug 11, 2022 at 8:07
  • 1
    If this question is about ensuring the input is sorted before applying the binary search, then forget it: it makes no sense to first ensure it, as that costs more than doing the binary search. If you're going to scan the input for checking it is sorted, you might as well use that opportunity to find the searched value during that scan, and then the binary search is not needed anymore. So either way, it is not helpful to check if the input is sorted. It must be sorted. If this cannot be guaranteed, forget about binary search. Commented Aug 11, 2022 at 9:47
  • 1
    @trincot: OP should settle this. Commented Aug 12, 2022 at 8:50

2 Answers 2

2

To check "sortedness", you need to compare all pairs of successive elements. You cannot do faster than O(N) because every element counts.

Your function always takes O(Log(N)) time, because it halves the interval until it becomes a singleton. But of course, the returned value can be meaningless.


The function performs a three-way comparison, so in many cases two tests per iteration. Even though comparison for equality can cause early termination, it is unsure that this is faster than a version with two-way comparisons only.

For a O(Log(N)) process, efficiency is not so critical.

Sign up to request clarification or add additional context in comments.

Comments

1

There are two scenarios to consider. Either:

  • The caller of the function guarantees that the list is sorted. In that case it is not necessary to have it double checked. It is then a documented requirement of your function, and the caller will stick to it. You can do the binary search assuming the list is sorted. This is known to have a time complexity of O(log𝑛)

or:

  • It is not 100% guaranteed that the list is sorted. In that case it makes no sense to check that it is sorted and then perform the binary search, because when you do that check, you have to visit every element of the list, which would give you the opportunity to immediately find the searched value in that list. So again, it would not be necessary to check that the list is sorted. Instead you would just use that time to actually find the value with a linear search that does not need the input to be sorted. Here the time complexity is O(𝑛).

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.