Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Description
Write a code that returns the number of unsuccessful attempts made to search for an element in the array using Binary search.
The code should
Take the size of the array as an input from the user
The elements of the array as an input from the user
The key you are searching for as an input from the user
Sample Input:
5
2 3 4 5 8
8

Sample Output:
2

What I have tried:

Java
import java.util.*;
 class Source {
   public int getBinarySearchUnsuccessfulComparisonCount(int[] inputArr, int key) {
   // Write your code here
  }
   public static void main(String args[] ) throws Exception {
       Source bs = new Source();
       Scanner scanner = new Scanner(System.in);
       int size = scanner.nextInt();
       int array[] = new int[size];
       for (int i = 0; i < size; i++) {
           array[i] = scanner.nextInt();
       }
       int key = scanner.nextInt();
       System.out.println(bs.getBinarySearchUnsuccessfulComparisonCount(array, key));
 }
}
please provise solution for this problem and explain how to solve
Posted
Updated 5-Feb-23 6:41am
v2
Comments
Richard Deeming 8-Nov-22 10:48am    
Based on your question, you have tried nothing. Nobody here is going to do your homework for you. If you really don't know where to start, then talk to your teacher.
Dave Kreskowiak 8-Nov-22 11:20am    
If only there was a search engine on the internet you could use to lookup how a "Binary Search" worked.
[no name] 8-Nov-22 12:26pm    
I agree with Richard MacCutchan, you have to solve it yourself, otherwise, no experience could be obtained from this simple task.

1 solution

Quote:
please provise solution for this problem and explain how to solve
We are not here to do your work for you. This is your assignment so you are expected to at least make the effort to solve it. As to how to do it, that should be explained in the question, but I am not sure what is meant by "the number of unsuccessful attempts made to search".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900