Friday, April 15, 2011

Sequential Search 2


import java.io.*;
class seqsearch
{
  public static void main(String args[]) throws IOException
  { searching x=new searching();
                DataInputStream in=new DataInputStream(System.in);
                int n=10,key=0;
                int a[]=new int[n];
                System.out.print("Enter number of elements ");
                n=Integer.parseInt(in.readLine());
               
                System.out.print("Enter elements");
               
                for(int i=0;i<n;i++)
                {
      a[i]=Integer.parseInt(in.readLine());
    }
   
    System.out.print("Enter number to be searched-");
   
    key=Integer.parseInt(in.readLine());
     
    int z=x.search(a,n,key);
    
     System.out.println("Element present at "+(z+1)+"location");
                 
   }  
  }
 
  class searching
  {           
                int search(int a[],int n,int key)
                {
                                for(int i=0;i<n;i++)
                                {
                                                if(a[i]==key)
                                                                return (i);
                 }
                 return 0;
                      
                }
                }