DSA - Linear Search

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Pnoctical No.

2
Oatc G 22
Aim VWiHe o pognom for Lineon 5conch
5o1twaoe tauiced Tova Online Compilen
Poognom Code
Public closs Lineonseonchi
public StotHcnt lineanstanch (int onrayC1,nx
int n Onraj.length
fon Cin iO, i<n, i++)
F ConnayC1 )
petunn
3
netunn-1,
3
Public static void main5tning ang sC3)
Tn onna yL1 f2, 4,0, 9 ,
int x 1,
int nesult linean 5eanch (onnaj x
fCoesult =-1)
Sjstem.out.pnintlo C"Elemenl nol found'")
else
Sy5tem.out.printlh Elemend found at idex: psoH),

Output Elemen+ou nd ot index: 3 e

Conctusion:
Linean 5canch is avesimple
5eaeehing a40nih m.T cycles +hmough
he elemenls unHl+he 9utyis aund.
clASSMAte
Page
Paacicol No.22

Aim Woite a pnoqnam fon linear 5eanch.


so-tuoaee Nequined Java Online CAmpilen
heon incan 6eaech is a5equenfio lseandhing
alaaiaithm wheme westant rom one end and
check evceny elemeq offhe lisl un he desiol
elemraE is fauad
Alganinm sbepiTnavense he amnay
54p Mateb he keyelemeat uith anayee
srpaTEe ekmat is fauacl, hetuna h
inde x posiHor 4t heenm element
slkpAI£ kyelemendt is not ound,ndunA-
Pnoqaam Cocde
puble class Linean Seanch i1
public satia iatlineanseanchlintaneayl,int z)
iatnEaoma leng
fonini=o, in, itHL
Elaeas i ] sx
neunn i
DeHino-1,
publicstatia vaid mainlstaing anqsC1)
2in annayI1e 2, 4,01,9t
iat x =1
o t aesult= ineanseanCh (anay x

5ysam.oulpniaHKCclemaal naf faund");


else
ystem.out.pnintlaEkeaen found atindexithsu
Ouput Flemeat found aB indax: 3
conclu6 iom Linean
t
seanch 1S a
veoy simple
cycles throough the elements untIthe seanching
gueny 15foundhayordho-

You might also like