6. Given the following Java code for a binary search algorithm, answer each ques
ID: 3753024 • Letter: 6
Question
6. Given the following Java code for a binary search algorithm, answer each question: boolean binarySearch( int [] values, int target, int low, int high )1 int midpoint - (first last) >> 1; if( low > high ) return false; if ( target -values [midpoint] ) return true; else if( target > values [midpoint] ) return binarySearch(values, target, midpoint + 1, high); return binarySearch (values, target, low, midpoint - 1); (a) (2 points) What is the best case complexity when searching for a single target item in values? Answer (2 points) (b) (2 points) What is the worst case complexity when searching for a single target item in values! Answer (2 points) (c) (3 points) What is the worst case complexity when searching for n target items in values! Answer (2 points):Explanation / Answer
Given code for binary search.
a. Best case occurs when the element is found in the first go. Best case complexity is O(1)
b. Worst case complexity occurs when all the elements have to be binary searched , complexity is O(log n)
c. In n^2 elements, complexity will be O(log n^2) which is O(log n)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.