Go to Top

Sunday 25 September 2011

Linear Search

Java Program for Linear Search : 

Given an array "a" containing integers, and a value "key", write a function which takes these as parameters and performs a linear search to determine whether "key" exists in "a" or not.

class lsearch
{
 static void search(int[]a,int key)
 {
     int p=-1,i;
     for(i=0;i<=a.length-1;i++)
     {
         if(a[i]==key)
         p=i;
      }
     if(p==-1)
     System.out.print("Search key '"+key+"' not found");
     else
     System.out.print("Search key '"+key+"' found at : Position "+(p+1));
    }
}

11 comments:

  1. That's a linear search.

    ReplyDelete
  2. Really a good program of Linear Search

    ReplyDelete
  3. So this is linear search. I didn't know. Thanks.

    ReplyDelete
  4. Wha should the input be?

    ReplyDelete
  5. This is a good page but students like me who need to study JAVA programming they need to know the questions..... So kindly plz post the questions also.

    ReplyDelete
    Replies
    1. Thank you for your valuable suggestion. Required changes have been made. Please feel free to revert back if you have any other concern.

      Delete
  6. Easy 2 understand gud!!!!!!!!👌👌👌👌👌👌👍👍👍👍👍👍🏆🏆🏆🏆🏆🏆🏆

    ReplyDelete
  7. import java.util.*;
    Class linearsearch
    {
    Void main ()
    {
    String cty[]=new string[5];
    Int std[]=new int[5];
    Int index=-1;
    String city;
    Scanner sc=new Scanner (System.in);
    System.out.println("Enter the city and STD code");
    for(int i=0;

    ReplyDelete

ShareThis