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
Should organizations that promote free trade have environmental or social checks and balances?
Step2247 [10]
it depends on the organization some organizations are more monopolizing than others so it really just depends on the organization that it talks about
6 0
2 years ago
Two routers, R1 and R2, connect using an Ethernet over MPLS service The service provides point-to-point service between these tw
liberstina [14]

Answer: b) R1 will connect to a physical Ethernet link, with the other end of the cable connected to a device at the WAN service provider point of presence.

.d) R1 will forward data link frames to R2 using an Ethernet header/trailer.

Explanation:

Here we have to understand what is MLPS. MLPS is a protocol which identifies the shortest route for the transfer of messages between routers instead of the longest route.

Here we are given a WAN which is a wide area network. Using layer 2 Ethernet service the frames are transmitted across the routers within the WAN.

Option A is incorrect as as connecting the other end of the cable to the R2 would reduce the functionality of the WAN.  Option C is incorrect as HDLC header trailer has no effect.

Option B and D are correct as Ethernet header/ trailer has the same size every frame and to maintain the functionality of the WAN one end must be connected to point of presence(PoP).  

5 0
3 years ago
Which osi layer defines the functions of logical network-wide addressing and routing?
IRISSAK [1]
Answer: The Network Layer
5 0
3 years ago
If you are to save something in your life what would it be<br>2.<br>and why?​
jeyben [28]
I would save memories in my opinion thats the most important thing.
4 0
3 years ago
Create a class called Book with the following properties using appropriate data types: Title, Author, numberOfPages, Create a se
tamaranim1 [39]

Answer:

I don't know the answer sorry

7 0
3 years ago
Other questions:
  • Earthquakes with magnitudes between 1.0 and 2.9 on the Richter scale are
    11·1 answer
  • Describe network in terms of the class computer lab
    14·1 answer
  • To place the caption at the top of the image, you will need to change the __________ in the
    5·2 answers
  • . Write a swift programming code to perform following tasks a) Declare a variable that stores the age of a person b) Declare a c
    5·1 answer
  • 5. ADD A STATEMENT OR STATEMENTS to the program on the following page (including constant and/or variable declarations if you wa
    6·1 answer
  • , 13 dB correspond to a power ratio of ....?
    14·1 answer
  • Most hard drives are divided into sectors of 512 bytes each. Our disk has a size of 16 GB. Fill in the blank to calculate how ma
    9·1 answer
  • Which cable would you check if you can't access any web pages?
    12·1 answer
  • Please help explain this calculator code.
    15·1 answer
  • EDI, ________, smart cards, and digital certificates are designed to support safe and secure online transactions.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!