0% found this document useful (0 votes)
17 views

GRP 11 Code

Uploaded by

Rush Taneja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

GRP 11 Code

Uploaded by

Rush Taneja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;
class linear
{
int arr[];
int num;
int i;
int search;
void accept()
{
System.out.println("Enter the size of array");
Scanner ss= new Scanner(System.in);
num =ss.nextInt();
arr=new int[num];
System.out.println("Enter the element of array");
for(i=0;num>i;i++)
{

arr[i]=ss.nextInt();
}
System.out.println("Enter the element you want to search");
search=ss.nextInt();
for(i=0;num>i;i++)
{
if(arr[i]==search)
{
System.out.println(search+" is present at this position "+i);
break;
}
}

if(i==num)
{
System.out.println("Element is not present in array");

}
}
}
class binary extends linear
{

int c, first, last, middle, n, search, array[];


void baccept(){
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " + middle);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
System.out.println(search + " isn't present in the list.\n");

}
}
class search{

public static void main(String s[])


{int ch;
Scanner ss=new Scanner(System.in);
binary l= new binary();

System.out.println("1.Linear Search");
System.out.println("2.Binary Search");
System.out.println("3.Exit");
System.out.println("Enter Your Choice (1-3)");
ch=ss.nextInt();
switch(ch)
{
case 1: l.accept();
break;
case 2: l.baccept();
break;
case 3:System.exit(0);

}
}

You might also like