Question
Pls do this is java. This is not a programming assignment. Write code or pseudocode. Error is code fixed: changed arr[mid] < x to x
Pls do this is java.
This is not a programming assignment. Write code or pseudocode. Error is code fixed: changed "arr[mid] < x" to "x < arr[mid]". and the corrected inequality is "arr[i] <= x < arr[i+1]".
1. Binary search is usually written in the following way: // Precondition: arr[0..n-1] is in sorted order boolean binarySearch(int[ ] arr, int x) { return binarySearch(int[ ] arr, 0, arr.length-1, x); } boolean binarySearch(int[ ] arr, int left, int right, int x) { // search for x in arr[left..right] if (left > right) { return false; } else { int mid = (left + right) /
2; if (x < arr[mid]) { return binarySearch(arr, left, mid-1, x); } else if(x == arr[mid]) { return true; } else { return binarySearch(arr, mid+1, right, x); } } } Write the following version of the problem: find the index of arr such that x lies between arr[index] and arr[index+1]. // return i, such that arr[i] <= x < arr[i+1]. Let n = arr.length. // If x < arr[0], return -1. If x >= arr[n-1], return n-1. int binarySearch(int[ ] arr, x) { /* To do */ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started