Thursday 11 August 2016

Linear Search in java

import java.io.*;
class linear
{
    public static void main(String []args) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a[]=new int[10];
        int i,key,flag=0,n;
        System.out.println("Enter the no of elements");
        n=Integer.parseInt(br.readLine());
        System.out.println("Enter the Values");
        for(i=0;i<n;i++)
        a[i]=Integer.parseInt(br.readLine());       
        System.out.println("Enter the Search element");
        key=Integer.parseInt(br.readLine());
        for(i=0;i<n;i++)
        {
            if(a[i]==key)
            {
                flag=0;
                break;
            }
        }
        if(flag==0)
        System.out.println("Search Element found at location "+ (i+1));
        else
        System.out.println("Search Element Not found");
    }
}

       

No comments: