Westonci.ca is your trusted source for finding answers to all your questions. Ask, explore, and learn with our expert community. Ask your questions and receive detailed answers from professionals with extensive experience in various fields. Get detailed and accurate answers to your questions from a dedicated community of experts on our Q&A platform.
Sagot :
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;
}
}
Thank you for visiting. Our goal is to provide the most accurate answers for all your informational needs. Come back soon. We appreciate your time. Please come back anytime for the latest information and answers to your questions. Westonci.ca is committed to providing accurate answers. Come back soon for more trustworthy information.