DEV Community

Kenneth Quiggins
Kenneth Quiggins

Posted on

Binary Search

Welcome to this tutorial on implementing a Binary Search algorithm in Python. In this tutorial, we'll learn how to use the Binary Search algorithm to search for an element in a sorted list or array.

Algorithm

The Binary Search algorithm works by repeatedly dividing the search interval in half until the target element is found or the search interval is empty.

Here's the basic structure of the algorithm in Python:

def binarySearch(list, item):

    low = 0
    high = len(list) - 1

    while(low <= high):
        mid = int((low + high) / 2)  # check middle element
        guess = list[mid]
        if guess == item:  # found item
            return mid
        if guess > item:   # guess too high
            high = mid - 1
        else:              # guess was too low
            low = mid + 1
    return None
Enter fullscreen mode Exit fullscreen mode

Here, we define a function called binary_search that takes two arguments: an array or list arr and a target element target. We initialize two pointers left and right to the start and end of the array, respectively. We then enter a loop that continues until left is greater than right.

Inside the loop, we calculate the midpoint mid of the search interval using integer division. We then compare the target element with the middle element of the array. If they are equal, we return the index of the middle element. If the middle element is less than the target, we update the left pointer to mid + 1, effectively narrowing the search interval to the right half. Otherwise, we update the right pointer to mid - 1, narrowing the search interval to the left half.

If the target element is not found after the loop exits, we return -1 to indicate that the element was not found in the array.

Example

Let's test our function with some sample inputs:


arr = [1, 3, 5, 7, 9]
target = 5
result = binary_search(arr, target)
print(result)

Enter fullscreen mode Exit fullscreen mode

In this example, we define an array arr of integers and a target element target that we want to search for. We then call the binary_search function with these arguments and store the result in a variable result. Finally, we print the result to the console.

If we run this code, we should see the output 2, which corresponds to the index of the target element in the array.

Conclusion

In this tutorial, we learned how to implement the Binary Search algorithm in Python. We also learned how the algorithm works and how to use it to search for an element in a sorted list or array.

Complexity

The time complexity of the Binary Search algorithm is O(log n). This is because the search interval is halved at each iteration, effectively reducing the search space by half. The space complexity is O(1), since we only use a constant amount of extra space.

Top comments (0)