Suppose following numbers are sorted in an array A:32,51,26,84,63,21,11,54Using Binary search find the location of item 26,11 and 99.Answer text
Question
Suppose following numbers are sorted in an array A:32,51,26,84,63,21,11,54Using Binary search find the location of item 26,11 and 99.Answer text
Solution
Sure, let's start with the binary search process. But first, we need to make sure that the array is sorted in ascending order. The given array is: 32, 51, 26, 84, 63, 21, 11, 54. Let's sort it first:
Sorted array: 11, 21, 26, 32, 51, 54, 63, 84
Now, let's find the items using binary search:
-
Finding 26:
- Start with the middle element: 32. 26 is less than 32, so we will continue our search in the left half of the array.
- Now, the middle element is 21. 26 is greater than 21, so we will continue our search in the right half of the remaining array.
- Now, the middle element is 26, which is the number we are looking for. So, 26 is at the 3rd position in the sorted array.
-
Finding 11:
- Start with the middle element: 32. 11 is less than 32, so we will continue our search in the left half of the array.
- Now, the middle element is 21. 11 is less than 21, so we will continue our search in the left half of the remaining array.
- Now, the middle element is 11, which is the number we are looking for. So, 11 is at the 1st position in the sorted array.
-
Finding 99:
- Start with the middle element: 32. 99 is greater than 32, so we will continue our search in the right half of the array.
- Now, the middle element is 54. 99 is greater than 54, so we will continue our search in the right half of the remaining array.
- Now, the middle element is 84. 99 is greater than 84, so we will continue our search in the right half of the remaining array.
- Now, we have no more elements left in the array. This means that 99 is not present in the array.
Similar Questions
Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.
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.
Arun is tasked with developing a binary search algorithm to efficiently locate the index of the first occurrence of a number divisible by 3 in a given sorted array of integers. Write a program to help Arun navigate the array elements and locate the required index. If a number divisible by 3 is found, return its index; otherwise, gracefully handle the absence of such a number.Input format :The first line consists of an integer n, representing the size of the array.The second line consists of n space-separated integers representing the elements of the sorted
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
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.