Java Blog

 
 
 

Image

import java.io.*;

class search
{
      String str;
      int key,size,seaArr[];

      public void getdata()
      {
             System.out.print(\"Enter how many data you want to enter : \");
             System.out.flush();
             try{
                 BufferedReader obj=new BufferedReader(new 

InputStreamReader(System.in));
                 str=obj.readLine();
                 size=Integer.parseInt(str);
                 seaArr=new int[size];
                 for(int i=0;i<size;i++)
                   {
                       System.out.print(\"Enter element at \"+(i+1)+\"th 

position  :  \");
                       System.out.flush();
                       str=obj.readLine();
                       seaArr[i]=Integer.parseInt(str);
                   }
                }
            catch(Exception e)  {}
      }

      public int LinSrch()
      {
            System.out.println(\"=====LINEAR SEARCH=====\\n\");
            getdata();
            System.out.print(\"\\nEnter Search Key : \");
            System.out.flush();
            try{
              BufferedReader obj=new BufferedReader(new InputStreamReader

(System.in));
              str=obj.readLine();
              key=Integer.parseInt(str);
                for(int i=0;i<size;i++)
                    {
                          if(seaArr[i]==key)
                               return(i+1);
                     }
                 }
             catch(Exception e) {}
             return(0);
        }    
}

class  linSea
{
    public static void main(String args[]) 
    {
           search o1 = new search();
           int result;
           result=o1.LinSrch();
           if(result==0)
                 System.out.println(\"\\nSearch Not Found\");
            else
                 System.out.println(\"\\nSearch is Located at \"+result+\" 

Position\");
    }
}

 
 
Didn't find what you were looking for? Find more on Program of linear search