Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.
Question
Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.
Solution
Sure, here is a simple Python program that demonstrates the use of Binary Search to search a given element in a sorted array in ascending order.
def binary_search(arr, low, high, x):
# Check base case
if high >= low:
mid = (high + low) // 2
# If element is present at the middle itself
if arr[mid] == x:
return mid
# If element is smaller than mid, then it can only
# be present in left subarray
elif arr[mid] > x:
return binary_search(arr, low, mid - 1, x)
# Else the element can only be present in right subarray
else:
return binary_search(arr, mid + 1, high, x)
else:
# Element is not present in array
return -1
# Test array
arr = [2, 3, 4, 10, 40]
x = 10
# Function call
result = binary_search(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")
This program first defines a function binary_search that takes a sorted array arr, the lowest index low, the highest index high, and the element x to be searched. It then checks if the element is present at the middle of the array. If it is, it returns the index. If the element is smaller than the middle element, it performs the search on the left subarray. If the element is larger, it performs the search on the right subarray. If the element is not found, it returns -1. The program then tests this function with a test array and a test element.
Similar Questions
Write a function that performs binary search on a sorted array.
Alex wants to create a program to search a target value in a sorted array. The program should input the array size and elements, as well as the target value. Utilizing binary search, it should determine if the target is present, and if so, provide the index. If not, display a message indicating its absence.Assist Alex in completing the program efficiently.Input format :The first line of input consists of an integer N, representing the size of the sorted array.The second line consists of N space-separated integers, the elements of the sorted array in ascending order.The third line consists of an integer target, the value to search for in the array.Output format :If the target is present in the array, print "The target value X is present at index Y", where X is the target element and Y is the index position (index starts from 0).If the target is not present, print "The target value X is not present in the array", where X is the target element.
The following steps are required to implement the binary search for an array sorted in ascending order.1) 1)Initialize Pointers: - Set two pointers, `low` and `high`, initially pointing to the start and end of the array, respectively. 2) Calculate Midpoint: - Calculate the midpoint index using the formula `mid = (low + high) // 2`. 3) Compare Midpoint Element: - Compare the element at the midpoint with the target element you are searching for: - If the midpoint element is equal to the target, the search is successful, and you can return the index. - If the midpoint element is less than the target, update `low` to `mid + 1` and continue the search in the right half. - If the midpoint element is greater than the target, update `high` to `mid - 1` and continue the search in the left half. 4) Repeat: - Repeat steps 3-4 until the `low` pointer is greater than the `high` pointer. This means the search range has become empty. 5) Return Result: - If the target element is found, return its index. If the target is not found, return a sentinel value (e.g.,-1) to indicate that the element is not in the array.Which steps would require modification if the array were sorted in descending order? Step 2 Step 4 Steps 2 and 4 No modification in steps
What is the initial step in executing a binary search on a sorted array?*1 pointA. Traverse the entire array from start to end.B. Compare the target value to the middle element.C. Sort the array in ascending order.D. Calculate the average value of the elements.
Given a sorted array A of size N, along with an integer target K, implement Binary Search to find the target K in the given array.Input FormatThe first line of input contains an integer N - the size of an array and target K. The second line contains the elements of the array.Output FormatFor each iteration of Binary Search, print the values of low, high, and mid. At the end, if the target K is found in the array, print "True" otherwise, print "False".Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput 19 121 4 6 7 10 11 12 20 23Output 10 8 45 8 6TrueInput 210 213 5 8 11 15 17 19 23 26 30Output 20 9 45 9 75 6 56 6 6False
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.