Professional Documents
Culture Documents
Binary Search and Linear Search
Binary Search and Linear Search
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int []a=new int[100000];
System.out.println("Enter the number of element you want to insert");
int n=sc.nextInt();
System.out.println("Enter the "+n+" elements\n !!Warning!! Enter the
elements in ascending order");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int first=a[0];
int last=a[n-1];
int ch;
while(true){
System.out.println("Enter the element you want to search");
int key=sc.nextInt();
System.out.println("Enter your choice...\n 1. Linear search\n
2. Binary search");
ch=sc.nextInt();
switch(ch){
case 1: Linearsearch(a,key);
break;
case 2: Binarysearch(a,first,last,key);
break;
case 3: System.exit(0);
break;