Binary search in Python codeBinary search in Python code

Binary search is a popular searching algorithm that efficiently locates an element in a sorted list. This algorithm is particularly useful when searching large data sets. In this blog post, we will discuss how to implement binary search in Python.

What is Binary Search?

Binary search is a searching algorithm that works by repeatedly dividing a sorted list in half until the desired element is found. It is an efficient algorithm that has a time complexity of O(log n). This means that the time it takes to search for an element in a list of size n is proportional to the logarithm of n. In other words, binary search is much faster than linear search, which has a time complexity of O(n).

Improve Performance and Reduce Environmental Impact with Binary Search

The Binary Search Algorithm in Python

Let’s begin by looking at the binary search algorithm in Python. The algorithm takes three arguments: a sorted list, the value to search for, and the indices of the start and end of the list. The function returns the index of the desired element if it is found, and -1 if it is not.

Here is an implementation of the binary search algorithm in Python:

def binary_search(arr, x, start, end):
    while start <= end:
        mid = (start + end) // 2
        if arr[mid] == x:
            return mid
        elif arr[mid] < x:
            start = mid + 1
        else:
            end = mid - 1
    return -1

In this code, we use a while loop to iterate through the list until the desired element is found. We calculate the middle index and check if the middle element is equal to the value we are searching for. If it is, we return the index. If not, we check whether the middle element is greater than or less than the value we are searching for. If it is greater, we update the end index to the index before the middle index. If it is less, we update the start index to the index after the middle index.

Binary Search in Python Code

Now that we have seen the binary search algorithm in Python, let’s look at how we can use it in our code. First, we need to have a sorted list to search through. Here is an example of a sorted list:

my_list = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

To search for a value in this list, we simply call the binary_search function with the list, the value we are searching for, and the indices of the start and end of the list:

result = binary_search(my_list, 11, 0, len(my_list) - 1)
if result != -1:
    print("Element is present at index", str(result))
else:
    print("Element is not present in array")

In this code, we are searching for the value 11 in the list. We pass the list, the value 11, and the indices of the start and end of the list to the binary_search function. If the value is found, the function will return the index of the element, which we print to the console. If the value is not found, the function will return -1, which we handle by printing a message indicating that the element is not present in the array.

Conclusion

In this blog post, we have discussed how to implement binary search in Python. We have covered the binary search algorithm in Python code. We have seen that binary search is an efficient algorithm that can be used to search large data sets quickly. By using this implementation we have provided, you can easily search through a sorted list to find the element you are looking for. We hope that this guide has been helpful to you and that you now have a complete understanding.