1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Roman55 [17]
2 years ago
12

Let's implement a classic algorithm: binary search on an array. Implement a class named BinarySearcher that provides one static

method named search. search takes a SearchList as its first parameter and a Comparable as its second. If either parameter is null, or if the SearchList is empty, you should throw an IllegalArgumentException. SearchList is a provided class. It provides a get(int) method that returns the Comparable at that index, and a size method that returns the size of the SearchList. Those are the only two methods you should need! search returns a boolean indicating whether the passed value is located in the sorted SearchList. To search the sorted SearchList efficiently, implement the following algorithm: Examine the value in the middle of the current array (index (start + end) / 2) If the midpoint value is the value that we are looking for, return true If the value that we are looking for is greater than the midpoint value, adjust the current array to start at the midpoint if the value that we are looking for is less than the midpoint value, adjust the current array to end at the midpoint Continue until you find the value, or until the start reaches the end, at which point you can give up and return false This is a fun problem! Good luck! Keep in mind that every time you call SearchList.get that counts as one access, so you'll need to reduce unnecessary accesses to pass the test suite.
Computers and Technology
1 answer:
yKpoI14uk [10]2 years ago
7 0

Answer:

Hope this helped you, and if it did , do consider giving brainliest.

Explanation:

import java.util.ArrayList;

import java.util.List;

//classs named BinarySearcher

public class BinarySearcher {

 

//   main method

  public static void main(String[] args) {

     

//   create a list of Comparable type

     

      List<Comparable> list = new ArrayList<>();

     

//       add elements

     

      list.add(1);

      list.add(2);

      list.add(3);

      list.add(4);

      list.add(5);

      list.add(6);

      list.add(7);

     

//       print list

     

      System.out.println("\nList : "+list);

     

//       test search method

     

      Comparable a = 7;

      System.out.println("\nSearch for 7 : "+search(list,a));

     

      Comparable b = 3;

      System.out.println("\nSearch for 3 : "+search(list,b));

     

      Comparable c = 9;

      System.out.println("\nSearch for 9 : "+search(list,c));

     

      Comparable d = 1;

      System.out.println("\nSearch for 1 : "+search(list,d));

     

      Comparable e = 12;

      System.out.println("\nSearch for 12 : "+search(list,e));

     

      Comparable f = 0;

      System.out.println("\nSearch for 0 : "+search(list,f));

     

  }

 

 

 

//   static method named search takes arguments Comparable list and Comparable parameter

  public static boolean search(List<Comparable> list, Comparable par) {

     

//       if list is empty or parameter is null the throw IllegalArgumentException

     

      if(list.isEmpty() || par == null ) {

         

          throw new IllegalArgumentException();

         

      }

     

//       binary search

     

//       declare variables

     

      int start=0;

     

      int end =list.size()-1;

     

//       using while loop

     

      while(start<=end) {

         

//           mid element

         

          int mid =(start+end)/2;

         

//           if par equal to mid element then return

         

          if(list.get(mid).equals(par) )

          {

              return true ;

             

          }  

         

//           if mid is less than parameter

         

          else if (list.get(mid).compareTo(par) < 0 ) {

                 

              start=mid+1;

          }

         

//           if mid is greater than parameter

         

          else {

              end=mid-1;

          }

      }

     

//       if not found then retuen false

     

      return false;

     

  }

 

 

}import java.util.ArrayList;

import java.util.List;

//classs named BinarySearcher

public class BinarySearcher {

 

//   main method

  public static void main(String[] args) {

     

//   create a list of Comparable type

     

      List<Comparable> list = new ArrayList<>();

     

//       add elements

     

      list.add(1);

      list.add(2);

      list.add(3);

      list.add(4);

      list.add(5);

      list.add(6);

      list.add(7);

     

//       print list

     

      System.out.println("\nList : "+list);

     

//       test search method

     

      Comparable a = 7;

      System.out.println("\nSearch for 7 : "+search(list,a));

     

      Comparable b = 3;

      System.out.println("\nSearch for 3 : "+search(list,b));

     

      Comparable c = 9;

      System.out.println("\nSearch for 9 : "+search(list,c));

     

      Comparable d = 1;

      System.out.println("\nSearch for 1 : "+search(list,d));

     

      Comparable e = 12;

      System.out.println("\nSearch for 12 : "+search(list,e));

     

      Comparable f = 0;

      System.out.println("\nSearch for 0 : "+search(list,f));

     

  }

 

 

 

//   static method named search takes arguments Comparable list and Comparable parameter

  public static boolean search(List<Comparable> list, Comparable par) {

     

//       if list is empty or parameter is null the throw IllegalArgumentException

     

      if(list.isEmpty() || par == null ) {

         

          throw new IllegalArgumentException();

         

      }

     

//       binary search

     

//       declare variables

     

      int start=0;

     

      int end =list.size()-1;

     

//       using while loop

     

      while(start<=end) {

         

//           mid element

         

          int mid =(start+end)/2;

         

//           if par equal to mid element then return

         

          if(list.get(mid).equals(par) )

          {

              return true ;

             

          }  

         

//           if mid is less than parameter

         

          else if (list.get(mid).compareTo(par) < 0 ) {

                 

              start=mid+1;

          }

         

//           if mid is greater than parameter

         

          else {

              end=mid-1;

          }

      }

     

//       if not found then retuen false

     

      return false;

     

  }

 

 

}

You might be interested in
Your friend just gave you his old laptop. Whenever you turn on the laptop, though, a black screen appears and asks you to enter
Anastasy [175]

Answer:
C is the answer!

Explanation:

A hard drive contains your operating system which can include your settings and preferences that you included in your device. But it mostly is a storage device so we know it WOULD NOT BE (B.

WOULD NOT BE D beacause powering it back on wouldnt do anything but bring you back to the start.

WOULD NOT BE A beacuse when taking out the battery then putting it back in would just be like turning it off then back on beacuse that just hold the power to a device.

IT WOULD BE C, C IS THE ANSWER beacuse a CMOS batttery contains the main system settings and  also contains information so i think C would be the best answer for this choice.


Hope this helps!
xx <3

7 0
2 years ago
How to delete account on this
qwelly [4]

Answer:

Explanation:

Go and edit your profile then go on prefernces you will see that its written delete my account and that's how you delete your account on brainly.com and if you want to delete you account on phone then

Open your phone's Settings app.

Tap Accounts. If you don't see "Accounts," tap Users & accounts.

Tap the account you want to remove Remove account.

If this is the only Google Account on the phone, you'll need to enter your phone's pattern, PIN, or password for security.

Hope this helped you!

5 0
2 years ago
Read 2 more answers
What is a sound wave
solmaris [256]

Answer:

A wave of sound!

Explanation:

sound waves are sound but sound comes in different wavelengths

7 0
3 years ago
Read 2 more answers
What it the total resistance in a series circuit with two resistors of 100 ohms each?
snow_tiger [21]

Answer:

200Ω

Explanation:

In series circuits, you add the resistances.

3 0
2 years ago
What is the user requirements of an educational app
VARVARA [1.3K]
Ummmmm I don’t know but I wanna know now.....
7 0
2 years ago
Other questions:
  • Calculate the total number of bits transferred if 200 pages of ASCII data are sent using asynchronous serial data transfer. Assu
    5·1 answer
  • What do you call the number that is used as an index to pinpoint a specific element within an array?
    14·1 answer
  • Sam's manager would like him to create and distribute copies of a budget report for each department. The budget report should sh
    8·2 answers
  • What command would you run from a windows command line to test a computer's network stack?
    7·1 answer
  • 1. Consider a direct-mapped cache that can accommodate 8Mbytes from a main memory, that uses a 32-bit address and 32-byte blocks
    5·1 answer
  • You are almost finished updating a Website. As part of the update, you have converted all pages from HTML 4.0 to HTML5. The proj
    7·1 answer
  • The difference between page break and section brek​
    5·2 answers
  • What are the names of the components (each shown with a leader and a line) of a circuit shown in the diagram?
    7·1 answer
  • If you want to share information with individuals who are internal to your organization, which type of network would you want to
    6·1 answer
  • What is adb command???​
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!