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));
}
}
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));
}
}
That's a linear search.
ReplyDeleteReally a good program of Linear Search
ReplyDeleteSo this is linear search. I didn't know. Thanks.
ReplyDeleteWha should the input be?
ReplyDeleteThis 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.
ReplyDeleteThank you for your valuable suggestion. Required changes have been made. Please feel free to revert back if you have any other concern.
DeleteThanks for Java program for linear search .
ReplyDeleteNice
ReplyDeleteEasy 2 understand gud!!!!!!!!👌👌👌👌👌👌👍👍👍👍👍👍🏆🏆🏆🏆🏆🏆🏆
ReplyDeleteimport java.util.*;
ReplyDeleteClass 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;
Is it an icse program ?
ReplyDelete