0% found this document useful (0 votes)
85 views9 pages

Unit-5 Pps PDF

The document contains descriptions and algorithms for several sorting and searching techniques: 1) Quadratic equation solving algorithm in 3 steps 2) Finding minimum and maximum of an array in 9 steps 3) Checking if a number is prime in 10 steps 4) Linear search algorithm in 9 steps 5) Binary search algorithm in 11 steps 6) Selection sort algorithm using if statements and for loops

Uploaded by

Uday Kumar M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views9 pages

Unit-5 Pps PDF

The document contains descriptions and algorithms for several sorting and searching techniques: 1) Quadratic equation solving algorithm in 3 steps 2) Finding minimum and maximum of an array in 9 steps 3) Checking if a number is prime in 10 steps 4) Linear search algorithm in 9 steps 5) Binary search algorithm in 11 steps 6) Selection sort algorithm using if statements and for loops

Uploaded by

Uday Kumar M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Uni

t-
V

I
ntr
oduct
ionToAl
gor
it
hems

1.
Quadr
ati
cequat
ion

ALGORI
THM:

St
ep1:
st art
St
ep2:
r eadthea, b,cval ue
St
ep3:
ifb* b-
4ac>0t hen
root1=( -b+pow( (b*b-4*a*c),
0.5))
/2*a
root2=( -b-
pow( (b*b-4*a*c),
0.5))
/2*a
St
ep4:
ifb* b-
4ac=0t hen
root 1=root 2=- b/ (
2*a)
St
ep5:
Ot herwisePr intImagi naryroots.Gotost
ep
St
ep6:
pr intr
oot s
St
ep7:
st op

2.
Findi
ngmi
nimum andmaxi
mum ofanumber
:

Al
gor
it
hem

St
ep1:start
St
ep2:readn
St
ep3:ini
tial
izei=0
St
ep4:ifi
<ndoasf ol
l
ows.I
fnotgot
ost
ep
5Reada[i
]
I
ncrementi
Got
ostep4
St
ep5:mi n=a[0]
,max=a[
0]
St
ep6:ini
tial
izei=0

St
ep7:
ifi
<ndoasf
oll
ows.I
fnotgot
ost
ep8I
fa[
i]
<mi
n

Assignmin=a[i
]
I
ncrementi got
oStep7
Step8:pr
intmin,max
Step9:st
op

3.
Findi
nggi
vennumberi
spr
imeornot

ALGORITHM:
Step1:
star
t
Step2:
readn
Step3:
ini
ti
ali
zei
=1,
c=0
Step4: i
fi <=ngot ost ep5
I
fnotgot ost ep
10St ep5: initi
ali
zej=1
Step6: ifj<=1doast hef
oll
ow.I
fnogot
ost
ep7
i
)ifi%j==0i ncrementc
i
i)incrementj
i
ii
)got oSt ep6
Step7: ifc==2pr i
nti
Step8: incr ementi
Step9: got ost ep4
Step10: stop

4.
Linearsear
ch

PROBLEM DESCRI PTION:


Thelinearsearchismostsimpleserchingmet hod.I
tdoesnotexpecttheli
stt
obe
sort
ed.
Thekeywhi chi stobesear
chediscompar edwitheachelementoftheli
stonebyone.I
f
amat chexists,thesear
chist
erminated.Iftheendoflisti
sreacheditmeansthatt
he
searchhasf ail
edandkeyhasnomat chingintheli
st.

SYNTAXDESCRI PTION:
Thesyntaxofi
fst
ructur
eisasf
oll
ows:
i
f(expr
ession)
{
Stat
ements

else
{
Statements
}
Thesy nt
axoff orl
oopst
ructur
eisasfoll
ows:
for(expressi
on1;expr
ession2;expr
essi
on3)
{
Statements
}
ALGORI THM:
Mai nProgr am:
Step1: Start
Step2: Readt hev alueofn
Step3: fori=1t oni ncrementi nstepsof1Read
thev al
ueofi t
helementi ntoarray
Step4: Readt heel ement (x)tobe
searchedSt ep5:sear ch<--l
inear(
a,n,
x)
Step6: i
fsear chequal to0got ost ep7otherwi
segot
ostep8
Step7: printunsuccessf ul search
Step8: printsuccessf ulsear ch
Step9: stop
LinearSear chFunct ion
Step1: start
Step2: fori=1t oni ncrementi nstepsof1
Step3: i
fm equal tok[ i
]got ostep4ot her
wisegot
ostep2
Step4: returni
Step5: return0
Step6: stop

5.
Binar
ysear
ch

Toperf
ormthebinarysear
choper
ati
on
PROBLEM DESCRIPTION:
Bi
narysear
chisav asti
mprov
ementovert
hesequent
ial
sear
ch.Forbi
nar
ysear
cht
o
wor k,thei
tem i
ntheli
stmustbeinassort
edor
der.Theapproachemploy
edinthe
binar y
sear chisdiv
idandconquer
.Ift
heli
sttobesor
tedforaspecif
ici
tem i
snotsor
ted,
binar y
sear chfai
ls.

SYNTAXDESCRI
PTI
ON:

Thesy nt
axofifstruct
ureisasfoll
ows:
i
f(expression)
{
Statements
}
else
{
Statements
}
Thesy nt
axoff orl
oopstructur
eisasfoll
ows:
for(expressi
on1;expression2;expr
essi
on3)
{
Statements
}

ALGORITHM:
MainProgr
am:
Step1:
Start
Step2:
Readthev
alueofn
Step3:fori =1t oni ncr
ementi nstepsof1
Readt hev alueofithel ementi ntoarray
Step4: Readt heel ement(x)t obe
searchedSt ep5: search<--binary(
a,n,x)
Step6: i
fsear chequal to0got ostep7ot herwi
segotostep8
Step7: printunsuccessf ul search
Step8: printsuccessf ulsear ch
Step9: stop
BinarySear chFunct i
on:
Step1: start
Step2: i
nitiali
sel owt o1,hi ghton, testto0
Step3: i
flow<=hi ghrepeatt hroughst eps4t o9other
wisegotost
ep10
Step4: assi gn(low+high)/2t omi d
Step5: i
fm<k[ mi d]gotost ep6ot her wisegotostep7
Step6: assi gnmi d-1tohighgot ostep3
Step7: i
fm>k[ mi d]gotost ep8ot her wisegotostep9
Step8: assi gnmi d+1t olow
Step9: returnmi d
Step10: return0
Step11: stop

6.
Sel
ect
ionsor
t

PROBLEM DESCRI PTI


ON:
Thisi
st hesimplestmethodofsort
ing.I
nthi
smethod,t
osor tthedat
ainascending
th t
h
order
,the0 el ementiscomparedwithal
lot
hereements.Ift
he0 elementisfoundto
begreaterthanthecomparedelementthent
heyareint
erchanged

3.SYNTAXDESCRI
PTI
ON:

Thesy nt
axofifstruct
ureisasfoll
ows:
i
f(expression)
{
Statements
}
else
{
Statements
}
Thesy nt
axoff orl
oopstructur
eisasfoll
ows:
for(expressi
on1;expression2;expr
essi
on3)
{
Statements
}
ALGORI THM:
Step1: St art
Step2: Initil
iazethev ari
ablesI,j
,tempandar r[
]
Step4: Readt hel oopandcheckt heconditi
on.Ifthecondi
tionistruepri
ntthe
arrayel ement sandi ncr
ementt heIv al
ue.Elsegot ostep4
Step4: Readt hel oopandcheckt heconditi
on.Ifthecondi
tiontruethen
gotonextl oop.
Step5: Readt hel oopandcheckt heconditi
on.Ifthecondi
tiontruethen
gotoi fcondi t
ion
Step6: Ift hecondi ti
onif(
arr[i
]>arr[j
])i
st r
uethendot hefol
lowing
stepst emp=ar r
[i
]
arr[i
]=arr[j]
arr[j
]=tempSt ep
Step7: Incr ementTheJVal ue
Step8: per formt heloopoper ationf orthedisplay
ingthesortedelements.
Step9: pr i
ntt hesor t
edelement s
Step10: stop

7.Bubbl
eSor
t

PROBLEM DESCRI PTI ON:


Bubblesortisthesi
mpl estandoldestsort
ingt echnique.Thismethodt akestwo
el
ement s
atatime.Itcomparet hesetwoelements.Iffir
stel ementsislessthansecondone,they
are
l
eftundist
rurbed.I
fthef i
rstel
ementisgreatert hensecondonet hent heyareswapped.
Theprocedurecontinueswi t
hthenexttwoel ement sgoesandendswhenal lthe
el
ement s
aresort
ed.Butbubbl esortisani
neffi
cientalgor i
thm.Theor derofbubblesort
al
gorit
hm
i
sO(
n2)
.

SYNTAXDESCRI
PTI
ON:

Thesy nt
axofifstruct
ureisasfoll
ows:
i
f(expression)
{
Statements
}
else
{
Statements
}
Thesy nt
axoff orl
oopstructur
eisasfoll
ows:
for(expressi
on1;expression2;expr
essi
on3)
{
Statements
}

ALGORI THM:
MainPr ogram:
1.star
t
2.readthev alueofn
3.fori
=1t oni ncr
ementi nst
epsof1
Readthev alueofithelementintoarray
4.call
funct i
ont osort(bubbl
e_sor
t(a,
n))
5.fori
=1t oni ncr
ementi nst
epsof1
pri
ntthev alueofithelementinthearray
6.stop
BubbleSor tFuncti
on:
1.star
t
2.initi
aliselastt on
3.fori =1t oni ncrementinstepsof1
begin
4.initi
aliseext o0
5.fori =1t ol ast-1incrementinstepsof
1begi n
6.ifk[i]>k[i
+1]got ost ep7other wi
segot
ost
ep5
begin
7.assi gnk[ i
]t otempassi gnk[i+1]
tok[i]assi gnt empt o
k[i
+1]i ncrementex
by1end- i
f
endi nnerf orl oop
11.ifex! =0
assignl ast -
1t olast
endf orl oop
12.st op

I
nser
ti
onsor
t

7.
inser
ti
onsor
t

PROBLEM DESCRI
PTI
ON:

Thisisanin-
placecompar i
son-
basedsor ti
ngalgor
it
hm.Her e,asub-l
istismaintai
ned
whichisalwayssorted.Forexample,thel owerpar
tofanar r
ayismai ntai
nedt obe
sort
ed.Anel ementwhi chistobe' insert
'edinthissort
edsub- l
ist
,hast of i
ndi t
s
appropr
iat
eplaceandt heni
thastobei nsertedt
her
e.Hencet hename, 
insert
ionsort
.

Thearr
ayissear
chedsequent
ial
l
yandunsort
editemsar emov edandinser
tedint
othe
sor
tedsub-
li
st(i
nthesamearray)
.Thisal
gor
it
hm isnotsuitablef
orlar
gedatasetsas
2
i
tsaver
ageandworstcasecomplexi
tyar
eofΟ(n) ,
where n 
i
st henumberofit
ems.
1.Compar et hecurrentelementi ntheiterati
on(sayA)wi ththeprevi
ousadj acent
el
ementt oit
.Ifitisinordert hencontinuetheit
erati
onel se,
got ostep2.
2.Swapt het woelement s(
t hecurrentelement 
intheit
er at
ion(A)andt heprevious
adjacentelementt oi t
).
3.Compar eAwi t
hi t
snewpr ev
iousadjacentelement.Iftheyarenotinor derthen
proceedt ostep4.
4.Swapi ftheyarenoti nor derandr epeatsteps3and4.
5.Cont inuetheiteration.
Thedi
agram givenbel owwi l
lmakey ouunder st
andthisbetter.
Al
gor
it
hem:

St
ep1:
Ifi
tist
hef
ir
stel
ement
,iti
sal
readysor
ted.r
etur
n1;

St
ep2:
Picknextel
ement

St
ep3:
Compar
ewi
thal
lel
ement
sint
hesor
tedsub-
li
st

St
ep4:
Shi
ftal
ltheel
ement
sint
hesor
tedsub-
li
stt
hati
sgr
eat
ert
hant
he

v
aluet
obesor
ted

St
ep5:
Inser
tthev
alue

St
ep6:
Repeatunt
ill
i
sti
ssor
ted

You might also like