Thursday, July 17, 2014

Array - IV [ Linear Search ]


Linear Search

 Linear search is a sequential search method to find a specific data. In this search method that start from the beginning to end sequentially to search the given item.

Complexity for Linear search


For a list with n elements, the expected time is the same as the worst-case scenario, which is O(n). The average time will be O(n/2). However, if the list is sorted, the complexity becomes constant, O(1).



Source Code


import java.io.*;
    class Linear_Search
        {
            public static void main(String args[])throws IOException
                {
                    Linear_Search L=new Linear_Search();
                    BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
                    int Size, Num, Pos=-1;
                    System.out.print("\n\tEnter Size of Array :  ");
                    Size=Integer.parseInt(Br.readLine());
                    int Ar[]=new int[Size];
                    for(int i=0;i<Size;i++)
                        {
                            System.out.print("\tEnter Element at ["+(i+1)+"]");
                            Ar[i]=Integer.parseInt(Br.readLine());
                        }
                    System.out.print("\n\tEnter Number to Search :  ");
                    Num=Integer.parseInt(Br.readLine());   
                    System.out.print("\n\tNumbers in Array \t");
                    for(int i=0;i<Size;i++)
                        {
                            System.out.print("  "+Ar[i]);
                        }
                    Pos=L.Search(Ar,Num) ; 
                    if(Pos>=0)
                        {
                            System.out.println("\n\t"+Num+" is Present at "+(Pos+1)+"  Position");
                        }
                    else
                        {
                            System.out.println("\n\t"+Num+" is Not Present in the List");
                        }                       
                }
            int Search(int Ar[], int N)
                {
                    for(int i=0;i<Ar.length;i++)
                        {
                            if(N==Ar[i])
                                {
                                    return i;
                                }
                        }
                    return -1;
                }  
      }         


Output




          Enter Size of Array :  5
          Enter Element at [1]6
          Enter Element at [2]7
          Enter Element at [3]4
          Enter Element at [4]3
          Enter Element at [5]2

          Enter Number to Search :  4

          Numbers in Array     6  7  4  3  2
          4 is Present at 3  Position


          Enter Size of Array :  3
          Enter Element at [1]5
          Enter Element at [2]1
          Enter Element at [3]3

          Enter Number to Search :  7

          Numbers in Array     5  1  3
          7 is Not Present in the List


No comments: