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

Finally e maving al the diaK rom

8ousCee touc to deinatton tous

8 seaschin
the Operation to
Searehra sefens to

o given key item


indirg he location
Colle ction ot Jivem.Hem

IheTe
fovevery searck techniu
a outcomaa

Fond Suce emsul-


OtheruiI- UnAuccejul- o

Page 20

Seareh i5icien depcmds 3m3 fatars


collettion o tems

The Search algori hm beirg uesd

3: The eficionc 4«st wd jov determine

WhethR he earch uccAsjul o


Searchina 3Cempenems îeeded
Foy ever

Seareh iAt
a Key element
3 t {unction Option.
There o 3 Searching agorithma
1Linear Search Data i in n-ovdered ashim.

Binary Saavch Data i i n ordered fahion


3Fiboracei Seavchi Data is in ordered ahion

Linear Seavch 1- 1 Seaentiot eav


which raverae
Linear Search iAa techniue
givem îter
Abe to lecate
a r a euermtialy
ntNeAT Saarch, tse acc each eleMno

on array gne or Aeuemtiall omd ee

whether + desired element oT o

acceARd and deaiTed element no -found.


TAte asccetul then dy
ESficienc ünear 8ear chi
The time taram ()Ahe Toof CompaT iAO

made in
earchi a Te cord (or) tttm inthe

Ahe eicin the techniau


Tne Key tem prexmt in 4he locatiom
Ahe gven liA , 4ten arly one comporiADm
them t in the beak cak0)
T4the kan tem ia prexent im Ate locatin

he gven lia, them camporisan' hoe to b


mods them (+ i he

he 3ivem i s oCompa1AONA Reded

i n+1) a . so the average Co On)

lgovithm
lgosithm Linea carch (A, Tm, Ket)
CtureNotes.
mpu A a y C I havinR n elerments ond

elememE Ho be found
aaput Key elomem locaien in axyaA

ther usi it 7eturn '- (Not{ound)


or f=o to n-1 do

AC1 euals k
rekurn f locian found
Teturn \

Page 22
Page 22

Hexe A ar anra hauing Tumbt


elememt R e i a 4he earch item in iat
this
algori thm tnTekurm the locatien *
tem he earch Soumd oth K
Teturn -Sor unguccasful Aeareh
& Binary Seaveh
-
T4 i an eatremely ef$icient aigorithm
This Search technique earchea the jivem K tm

In minimum poibh comparriaona


Fo doig binaryeach, jirat we had to Aost h e

Qray elemen
the behind thia Hechnique i given belouu
ogic
) Firat find the midd element an aray

the middll elamemt t h key Ttem

Ca Li Te NOtes.In
a) 4 t i deired item them Aeach is 8ucee
b T t is l e than he desiwed item hen
seasch only im ihe 4 holf ot an a r r
c) T4 t ib greatex harm the dexirad tem they
Search i n he a" alf dt an arro

First value Mid value Lopt vaue

altok and half


an o r r o an aojN

Previous Year Questions on Binary Search


iv) Repeat ome BkepA unit an element Cor) îtem
ia found or exhausts in the Aeor eh area.

E4Sicieneg rary &axch ,-


In 4hi» algovithm every time e u Taduciq

he TOe CompariAU
X o tima te can divide aryo elements

by '2 upto
ou have elemen

do he log2

lo3,(2) lea,(u)
l e s leqN

The complanity
beat-Cask 4ime Ou
The uorat -

caw and ayrage ca time


Ceplenity ia o(logn)
hen Compared to inear 8earch. DTaD
Seavch ia eficient outhe arro elame's
shauld be rled be-fore earchv
Algori thm-
tHese is orted ama th le{t value o
Ond ight value ot n-t and the arey awèng n
no-ot elemem. key,is Ahe elemem 4o be foumd tn

ivem ist. T4 + W found (o) Succetul Tekuy


mid value Otheren
Unuccajul.
Algoriithm binary Aearch (A, n, keg)

hpu Ai sorted araj having 'n elumams and


Key ttm to be searchb
akpur: locatim Key item in ama ALI Y
- it is uwucceful
I mid, lost=o,
uhile
rightt=n-tN
left< rght do
mid eft+righr)|23
FAfmia] equala ke
seturn mid
e tf nidKey <

Set midmid+l
e
sel right mid-
En uhie loop es.ln
eturn i,

Serted aro 1elemmem ts omd ke s


Fiample

let-0, Tight -6,


Key-49 rekum mid i
O<6.T mid - O 3 Tetun 5
)
ACmid J- a[3]> 30:4s>tinatty o u earch elamemf
)30 q5 T> leftz mid*l ifourd at locah an o
et4
5
let4vight-6, kag-s
<6T mid- - 5
i) Afmi)-a{s]45usT|

Page 25

19SerEirg Techniues
w ww

AromgiT the elemenm ei the


ascendin order
OY n dexcendirg order
called Serhg.
Sovti techniques a
roadly claskified into
tuo ypes
let4,isht6, Kay-s
46T mid- t - 5
)Afmi-A[s]^45-4ST|

Page 25

9
w
Sertina
mmw
Techniues
romgi the elemems eitter ascending orde
in deAcemdir order is called Sortin.

Sorti techniues a sroasly clasified into


tuuo ypes

i intexnal Sorting
i) Exkernol
nteral Sarhig All 4he TeCords that o to be
orted in moim menAr

Eterral Sovtin Some AorH that Can't be perjorm-


m o i n memery nd maat be done di^k 6r)

tape his ype oSor au called aa Enternras Aortir

Sort

CctuIeNotes.in
na
Soring Eater TeMegea)
SortiresMergea)

Natutal
ealancad
Tuevtion Secion Echange pcly phaae
Sfwntion selectim Bauble
Shell Heap quickK
InseTion St
Jninertion 60rt Ahe Can be
devided nto

Othen Unorted ist. In eoeh Pos the firat


elemenE ounAoTted ist ia tranafaaed to

sosted subist ay enti q i in aPP TOPriate


(Or) PopeT Placa

ProceSt
AC1 b self ia tivallu Aorted
ither before or affea

propen plac
in Alo]
AC21 is inuented into %

DE Ahat is beove A[o), behaeen ALOJu At

Au
Thi Proc% isepeated al elemenK
in ATHes ordentes. 1 n
Enample - ray t
i
The Jiven om

a3, 18, 45, 18,32,16


a3 uih 18 , a3>18 Cdit
Compayi
be false o theve i T change

plaCu
a3, T8, G5, 18, 32,16
8387
pax a Gs unth &rhed ust
Cempari
78 45 omd 83745 (F) So
785(T) a3, ?, 18,18,32,10
a345(F) a3, 45, 78, 18,32,o

Previous Year Questions on Insertion Sort


Questions Bank on lInsertion Sort

Page 27

P s 3: COmpariTA18 sith the sorted


ublis a34s 818
18>18 T » 23,G5,2 ,15, 32,10
4s18T a3, ?,u5,15, 310
318T a3, u5, 75,32,10
Fiaty 18 ,a3, us, 1S,32,

Comparing 32 h e orted u b s
18,23,us amd 15
, tt Conditian uiE
true change tne pokition o ge
elemenF
15>3T 18,23, us,2 T5,10
uS 3 2 T 18, 23 2 ,us,15, 10
13, 23,32, 45,15, 10 ^Na chans
o th the oted ubli st
Comparo
18,23, 3 , us amd 15.
1S >10 T 18,23, 32,1S,? 15
1 2332,n us,15s
3210 T 1,23, 2.345,15
a3 o T
18,2,23,32,4S,1ns
18 10 Te 2, 18,2332-,45. 15
lo, 18, 23 3,4S, 15.
finally Ou sorFed aYO eluma OJl
0, 1,2, 3 u S , 15.
Algori thm
Algorithm inserstion. 3ort CA,n)
StaThr
amd
aRead al the elemenh o an arta
Stose Ahem nto
an arajALS
3 Repeat Step omd Step 6 Hor K 2,3.4----D

S e r tempA K] and PAS=K


5 Repeat onile PoS>o temP < ACPos-i
0)Set A{os AlPos-J,
bSet pos: PoS-1
End lop
6 Set ACPos: =Bemp ,
End Step 3 loop

End.
Time complenut

Noo P a e eauired for nelemens ia n-i

TTeede d in cach ay
NO oF Cempanin on
in uorst cau

TCn) < (n-d 4 (n-)

(n-
S(Y-2znan)
OCn).
worat-Cage
T ot)|
Compleitt
Sele ction Sort
Inirat atep the malleat elumamt eanc

in the ist, oncau he moltest elumarns


elemon 19
oumd, + a echanged nth the

-firat poRitian.
Now the Us iy divided into tu paTt3 .

One i srted &s other i nsrted liat.


Findout he Asa llasH elamemtrom 4he
nsorted ist ond t ia enchange with
Ahe
skavi" Pohtian unosted liab, after
Hhar T uil added n to 8orted list
hii mo Ca i rted mt au Ahe elumments

a ored

Eampl' The 3ven arya a is

LCCt 93,18,us, 18, 32,10


Fas. Findihe minimum elument Arom

is lo, it a erchampe
t the firat elumemt

(83. 78, 49, 18.32,0oin


lo,:18,45, 18,32a3

minimun euman
ecaPt
Fndi the
element i
18, it a enchomge ui th
4elamemt in mrted i a ) nith
aTelamen}t

Previous Year Questions on Selection Sort


Questions Bank on Selection Sort

Page 30

o,18us, is, 32, a3


T

0, 18, us, 18,22,23


minimum elemramt eacept ira
tus eumanh becaud hoe u s ou alea

red Nou minimum s a3 chamge


wth 3d poition alu.
10, 18,s) T8,32,2
Pos lo, t,a3, T8,3,us:
Minmum eumamE iA 31, enehame *hiy
uh h PORition valua 1s

I0, 18,23, t,3,us


min
I0,18123, 3,18,us
Pas S:- minimuy is G5, enchamge unh 5m
Poition volua 8

Lect ulOsas, 37.13 uS


min
,18,23,32,45, 18.

Io18, a9, 32,U5,18


Time cem plenity
NoofelemenA

Comparis0) n each fos 4 - mA


No
Time TeqJuises doreAecution is

S(n-Y
Algorithm:
Algorithm Seletion 01t (An)
Start
a Read al he elemen &an aTaomd
kore he elumen into aray AC1
3-
Repeat stepG and step s -jar k=1,2 n
Call min(A. K,D, loc)
6 Tatr
amge nik] and A[loc]
Se
temp =
A[k)
LLac tomp
End o Step3 lcop

Butble Sr
Bubble Sot a auko calld a Snki (o) Exchage Sot
nbubd ort , each lermeoneia compared
adgacen* elomamt
h e econd On
largen Aham
T4he firat elament
he elumemh intenchaungeA
then the postion
i) Othevie, ihe potitien h e elumens on

chamged
The Soma
o lefF Jor Cmpaia
Aftn the 4t o he arga elm ent a Pacad

in - " locaion.
16
Sep
1 is checKiT t h 4, Sinew 1>a, enchange elumamls

as8 3 9 16
Pas 3
Step
y chetking ls th 6, a>6 (False), Nochog
839 1.16
Sfep
Chetkig usth 8, 678(Falbe), No choge

SFep as 8 3
s chtkima usith 3, 8>3,Erchoge & 3

a 63 8 14 1
Step a 6 3 8 4 16

8 i chetkimg tith , &>a(false No chorg


41 6
86 3 8
Skep5 a 638
9
A CheckinRith u, q4, erctareu
as3 8 4
pas
step) a63 8 G
a is checki t h 6, a>c (Fal«) , No Charge

a6 3 8

3 8 IG

6 Checkig uHh 3, 6>2,Echomea 3


a3 6 84 1 6
Page 33

8 R 16
SEep 3 t a3 6
6 Che Cki»g i i h 8, 678 (Fal») No chomR

a 3 6 84 16
step
8 checka sth , 8>u (true) , Eacbarg8 by
a 6 u& IG
Pas S
SFeP-
Checrima sith 3, a>3 (Fale),
Ne chong
3G 8 G

SteP
ChetkiTg uith 6, 376(Fai ), Nocharg9

Sep3-
s by
Checria utb 4, 6>a,Eicharge
6
a3 S 8
Le
pas'-
StePl

c h e c k i a uith 3, a>3 (aw) N charee


Sep-

unth , 3>u (Faa), No chorgj


check.i

Stepl
3>3(Fal«s)
No Chang
& V checkim rih 3,
å 3 4 G8 IG
Stept
3a>3(Falzs) No chamg)
& checkig Hh 3,
3 4 G8 .

Page 34

lgori th:
lqoithm bubdSort (A,n)
Start
a Read aray elumenh and inant Imto g7ray at
3. set :=o',

4 Repeat Step 5 and s P 1 un1 <D


5 Set jt= o
6 Repea Step 7 until 3« (n-i)-i
ACII>Ali+1) 4hen

Set temp : ALIJ;

ACi+: temp,

End sters lesp


End SteP 4 ooP

End
Time csnplunitt-
oJive
FToh Ate enassopla x Ioh the Ashed
elumams ou 8.
nihe pos , So ihe mo
Tola pos uired for n elemen-S A
o
-1

Totad timas Caplet i>

TO S - 9 4 (n-)

(n-nY
-2n+1)
Aorat-caxsTm
+timas Comdeai.
On
sorti Teehngue Timu cemple
Sovin Technigsue
Inaesthon &ot oCn) o()
Sclechon Soxt O() o() Oln)
Bubble Sor O ol)
ofnegm) o)
o(nagn)
Rodin Sst O(nx) O(nx) DCnK)
Mevge oT o(nlogm) onlogn) olnlagn)
olnlegn onieg
Heap Sot onlgn)
Search Techsiyua, Timd Omplkn

Seaich Techniu BearCa

Linear Seai ch Otes Og


ina Search cg)
eNO

You might also like