Sequential search

 

// 1. Sequential search

import java.io.*;

import java.util.*;

 

class SequentialSearch

{

     public static void main(String [] as) throws IOException

     {

          Scanner sc = new Scanner(System.in);

          int i, ele, arrlen, pos=-1;

         

          System.out.print("Enter array length : ");

          arrlen = sc.nextInt();

         

          System.out.println("Enter array elements : ");

          int arr[] = new int[arrlen];

          for(i=0;i<arrlen;i++)

              arr[i]=sc.nextInt();

         

          System.out.print("Enter element to search : ");

          ele = sc.nextInt();

         

          for(i=0;i<arrlen;i++)

          {

              if(arr[i]==ele)

              {

                   pos = i+1;

                   break;

              }

          }

         

          System.out.print("Entered array is : ");

          for(i=0;i<arrlen;i++)

              System.out.print(arr[i]+"  ");

         

          if(pos!=-1)

              System.out.print("\nElement found at position "+pos);

          else

              System.out.println("\nElement not found in array");

     }

}


No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu