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

All Searching

Uploaded by

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

All Searching

Uploaded by

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

import java.util.

*;
class linear_search
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,s,f=0;
System.out.println("enter size array");
n=sc.nextInt();
int a[]=new int[n];//array create of n
size
System.out.println("enter the value");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("enter search value");
s=sc.nextInt();
for(i=0;i<n;i++)
{
if(a[i]==s)
{
System.out.println("search value is
present at position"+(i+1));
f=1;
break;
}
}
if(f==0)
System.out.println("search value not
present");
}
}
import java.util.*;
class binary_search
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n,i,s,f=0,li,hi,mi;
System.out.println("enter size of array");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter values of array");
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.println("enter the search value");
s=sc.nextInt();
li=0;
hi=n-1;
mi=(li+hi)/2;
while(hi>=li){
if(s==a[mi]){
System.out.println("value present at
position"+(mi+1));
f=1;
break;
}
else if(s>a[mi])
li=mi+1;
else
hi=mi-1;
mi=(li+hi)/2;
}
if(f==0)
System.out.println("value not found");
}
}

You might also like