1 / 4

Binary Search in Python

https://pythongeeks.org/binary-search-in-python/

ayushii12
Download Presentation

Binary Search in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Search in Python Creating a Binary Search in Python In the world of programming, searching for an element in an array is a common task that developers come across regularly. In such cases, a binary search algorithm can be used to search for an element in a sorted array in a fast and efficient manner. Binary search is a searching algorithm that follows the divide and conquer approach to search for an element in a sorted array. In this blog, we will explore how to create a binary search in Python. Binary search is an efficient searching algorithm that helps in searching an element in a sorted array by dividing the array into halves. Here’s how to create a binary search in Python: 1. Take an input array and an element to be searched. 2. Initialize two variables, ‘low’ and ‘high’, which represent the starting and ending index of the array, respectively. 3. Calculate the middle index of the array using the formula: middle = (low + high) // 2 4. Compare the middle element of the array with the search element. 5. If the middle element is equal to the search element, return the index of the middle element. 6. If the middle element is greater than the search element, search in the left half of the array by updating the ‘high’ variable to middle-1. 7. If the middle element is less than the search element, search in the right half of the array by updating the ‘low’ variable to middle+1. 8. Repeat the above steps until the search element is found or the array is exhausted.

  2. Let’s look at an example of binary search in Python: Example: Suppose we have an array of integers [1, 2, 3, 4, 5, 6, 7, 8, 9], and we want to search for the element 5 using binary search. Code Implementation: We will implement binary search in Python using a function that takes an array and an element to search as arguments. Here’s the code: def binary_search(arr, x): low = 0 high = len(arr) - 1 mid = 0 while low <= high: mid = (high + low) // 2 # Check if x is present at mid if arr[mid] < x: low = mid + 1 # If x is greater, ignore left half elif arr[mid] > x: high = mid - 1 # If x is smaller, ignore right half else: return mid

  3. # If we reach here, the element was not present return -1 In the above code, we first initialize the ‘low’ and ‘high’ variables to the start and end indices of the array, respectively. We then calculate the middle index of the array and compare the middle element with the search element. Based on the comparison, we update the ‘low’ and ‘high’ variables and repeat the process until the search element is found or the array is exhausted. Now, let’s call the function with the example array and search element: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] x = 5 result = binary_search(arr, x) if result != -1: print("Element is present at index", str(result)) else: print("Element is not present in array") Output: Element is present at index 4 Conclusion Binary search is a powerful algorithm that allows us to find a target value in a sorted list of items quickly and efficiently. By dividing the search space in half repeatedly, we can drastically reduce the number of comparisons we need to make, making it much faster than a linear search. In this tutorial, we’ve covered how to create a

  4. binary search function in Python step-by-step, and we’ve shown how it can be applied to solve different problems. With the help of this article, readers should now be able to create their own binary search functions and apply them to their own programming projects.

More Related