14. WRITE A PROGRAM that contains a recursive function to perform Binary Search15. (Hint: The function will need the sorted array, element to be searched, lower index and upperindex as arguments. Initially lower index will be 0 and upper index will be N-1, where N is thenumber of elements in the array. If lower index > upper index, return -1, indicating that elementis not present in the list. Otherwise, do the following:-16. Mid index is calculated as average of lower index and upper index.17. If Array’s mid index element equals item to be searched, return mid.18. If item < Array’s mid index element, call the function with upper limit changed to (mid index-1) and return the result.19. If item > Array’s mid index element, call the function with lower limit changed to (mid index+1)and return the result
Question
- WRITE A PROGRAM that contains a recursive function to perform Binary Search15. (Hint: The function will need the sorted array, element to be searched, lower index and upperindex as arguments. Initially lower index will be 0 and upper index will be N-1, where N is thenumber of elements in the array. If lower index > upper index, return -1, indicating that elementis not present in the list. Otherwise, do the following:-16. Mid index is calculated as average of lower index and upper index.17. If Array’s mid index element equals item to be searched, return mid.18. If item < Array’s mid index element, call the function with upper limit changed to (mid index-1) and return the result.19. If item > Array’s mid index element, call the function with lower limit changed to (mid index+1)and return the result
Solution
Sure, here is a step-by-step explanation of the program:
-
Define a function called "binarySearch" that takes four arguments: the sorted array, the element to be searched, the lower index, and the upper index.
-
Inside the function, check if the lower index is greater than the upper index. If it is, return -1 to indicate that the element is not present in the list.
-
Calculate the mid index by taking the average of the lower index and the upper index.
-
Check if the element at the mid index of the array is equal to the item to be searched. If it is, return the mid index.
-
If the item is less than the element at the mid index, recursively call the binarySearch function with the upper limit changed to (mid index - 1). Return the result of this recursive call.
-
If the item is greater than the element at the mid index, recursively call the binarySearch function with the lower limit changed to (mid index + 1). Return the result of this recursive call.
By following these steps, the binarySearch function will perform a binary search on the sorted array and return the index of the element if it is found, or -1 if it is not found.
Similar Questions
Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.
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.
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.
Write a function that performs binary search on a sorted array.
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
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.