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

Algorithm

The document outlines algorithms for inserting and deleting elements in a linear array, along with traversal methods. It also defines data structures such as stacks and queues, explaining their operations and applications. Additionally, it includes sorting techniques like bubble sort and searching methods including linear and binary search.

Uploaded by

aacron762
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)
6 views9 pages

Algorithm

The document outlines algorithms for inserting and deleting elements in a linear array, along with traversal methods. It also defines data structures such as stacks and queues, explaining their operations and applications. Additionally, it includes sorting techniques like bubble sort and searching methods including linear and binary search.

Uploaded by

aacron762
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

Rammurt

iEducat
ionSoci
ety'
s
St
.Xav
ier
s’Engli
shHi
ghSchool&[Link]
lege,
Manpada,
Thane(
w)

Subject:
-Comput
erScience–I
Topic–DataStr
uctur
e
Probablemar
ks–17

1-
“ALGORI
THM OFI
NSERTI
ONOFANELEMENT”

INSERT(LA,
N, K,ITEM)[
LAisali
neararraywit
hNelementsandKisaposi
ti
ve
t
h
i
nteger
ssuchthatK≤[Link]
gor
it
hm insertanel
ementI
TEM int
otheK posi
ti
oni
n
LA]

1. [
Ini
ti
ali
zeCount
er]SetJ:
=N

2. RepeatSt
eps3and4whi
l
eJ≥K

3. [
Mov
etheJthel
ementdownwar
d]SetLA[
J+1]:
=LA[
J]

4. [
Decr
easeCount
er]SetJ:
=J-
1

5 [
Inser
tEl
ement
]SetLA[
K]:
=ITEM

6. [
ResetN]SetN:
=N+1;

7. Exi
t

2-“
ALGORI
THM OFDELETI
ONOFANELEMENT“

DELETE(LA,N,K,ITEM)[
LAisali
neararr
aywit
hNelement
sandKisaposi
ti
ve
t
h
i
nteger
ssuchthatK≤[Link]
salgor
it
hm del
etesK el
ementf
rom LA]

[Link]
TEM :
=LA[
K]

2. Repeatf
orJ=Kt
oN-
1:

[
Mov
etheJ+1stel
ementupwar
d]SetLA[
J]:
=LA[
J+1]

3. [
Resett
henumberNofel
ement
s]SetN:
=N-1;

4. Exi
t
3-“
ALGORI
THM OFTRAVERSI
NG“

Her
eLAi
sal
i
nearar
ray.

LB–Lowerbound,
UB=Upperbound,

• St
ep1-[
Ini
ti
ali
zecount
er]

SetI
:=LB

• St
ep2-Repeatst
ep3and4Whi
l
eI<=UB:

• St
ep3–[
Visi
tel
ement
]

Appl
yPROCESSt
oLA[
I]

• St
ep4–[
Incr
ementcount
er]

SetI
:=I
+1

[
Endofst
ep2l
oop]

• Exi
t

4-WHATI
SSTACK?

• St
ackisal
i
neardat
ast
ruct
urewhi
chwor
ksonLI
FOor
der
.Sot
hatLastI
nFi
rst
Out.

• I
nstackel
ementi
sal
way
saddedatt
opofst
ackandal
sor
emov
edf
rom t
opof
t
hestack.

• Stackisusefuli
nrecursi
vefunct
ion,
funct
ioncal
l
ing,
mat
hemat
ical
expr
essi
on
calcual
ti
on,
reversi
ngthestr
ingetc.

5-WHATI
SQUEUE?

• Queueisal
soal
i
neardat
ast
ruct
urewhi
chwor
ksonFI
FOor
der
.Sot
hatFi
rstI
n
Fi
rstOut.

• I
nqueueel
ementi
sal
way
saddedatr
earofqueueandr
emov
edf
rom f
rontof
queue.

• Queueappl
i
cat
ionsar
einCPUschedul
i
ng,
DiskSchedul
i
ng,
I
OBuf
fer
s,pi
pes,
fil
e
I
O.

Fewi
mageswi
thanswer
sofpr
evi
ousy
earquest
ions”
#BUBBLESORT
LA[
1]-55, LA[
2]-
43, LA[
3]-
05, LA[
4]-
06, LA[
5]-
09

PASS1:
A)COMPARELA[
1]WI
THLA[
2]SI
NCE55>43EXCHANGE

43 55 05 06 09
B)NEXTCOMPARELA[
2]WI
THLA[
3]SI
NCE55>5EXCHANGE

43 05 55 06 09

C)COMPARELA[
3]WI
THLA[
4]SI
NCE55>6EXCHANGE

43 05 06 55 09

D)COMPARELA[
4]WI
THLA[
5]SI
NCE55>09EXCHANGE
43 05 06 09 55

NEW LI
ST:
-

PASS2:
A)COMPARELA[
1]WI
THLA[
2]SI
NCE43>05EXCHANGE

05 43 06 09 55

B)COMPARELA[
2]WI
THLA[
3]SI
NCE43>06EXCHANGE
05 06 43 09 55

C)05 06 09 43 55

NEW LI
ST:
-
PASS3:

A)COMPARELA[1]WI
THLA[
2]SI
NCE5<6 NOEXCHANGE
05 06 09 43 55
B)COMPARELA[2]WI
THLA[
3]SI
NCE6<9 NOEXCHANGE
NEW LI
ST:-05 06 09 43 55

PASS4:
THESORTEDARRAYI
S
05 06 09 43 55

LA,N ASCENDINGORDER
STEP1:
REPEATSTEPS2AND3FORI
:=1TON-
1

STEP2:
SETJ:
=1

STEP3:REPEATWHILEJ<=N-I
A)IFLA[
J]>LA[
J+1]
,THENEXCHANGE

LA[J]ANDLA[J+1]
[ENDOFI FSTRUCTURE]
B)[
INCREMENTOFTHECOUNTER]
SETJ=J+1

[
ENDOFI
NNERLOOP]
[
ENDOFOUTERLOOP]

STEP4:
EXI
T
#Li
nearSear
ch:
-
Example:
-
LA-11 22 33 44 55

I
TEM =33
LA[
N+1]=33

STEP1:
11 22 33 44 55 33
STEP2:
LOC=1
STEP3:
SINCELA[
1]=11!=33 SO LOC=2
SI
NCELA[2]!
=22!=33SO LAC=3
SI
NCELS[3]=33=33=ITEM
STEP4:
HENCEITEM =33FOUNDINLOCATION3.

#Al
gor
it
hm:
-
LA,
LOC,
ITEM

STEP1:
[INSERTITEM ATTHEENDOFLA]
SETLA[N+1]:
=ITEM

STEP2:[I
NITI
ALIZECOUNTER]
SETLOC:=1

STEP3:[SEARCHFORITEM]
REPEATWHILELA[LOC]
!=I
TEM:
SETLOC:=LOC+1
[
ENDOFLOOP]

STEP4:I
FLOC=N+1,
THEN:
SETLOC:
=0
[
ELEMENTNOTFOUND]

STEP5:
EXI
T

#Bi
nar
ySear
ch:
-
11 22 30 33 40 44 55 60 66 77 80 88 99
BEG MID END

I
TEM =40
BEG=1 END=13 MI
D=(
BEG+END)
/2=(
1+13)
/2=7

BEG END MI D
1:BEG=1ANDEND=13
MID=7
LA[
MID]
=LA[
7]=55

2.40<55,ENDCHANGE
END=MI
D-1
MID=1+6/ 2=3

3.40>30

BEG=MID+1
BEG=4
MID=4+6/2=5
LA[MI
D]=LA[5]=40

LOC=MI
D=5

#Al
gor
it
hm:
-
STEP1:
SETBEG=LB END=UB MI
D:=I
NT(
BEG+END)
/2

STEP2:
REPEAT STEP3AND4

WHI
LEBEG=ENDANDLA[
MID]
!=I
TEM

STEP3:
IFI
TEM<LA[
MID]
,THEN

SETEND:
=MI
D-1

ELSE

SETBEG:
=MID+1
(ENDOFI
FSTRUCTURE]

STEP4:
SETMI
D:=I
NT(
BEG+END)
/2

[
ENDOFSTEP2LOOP]

STEP5:IFLA[MID]
=ITEM,
THEN
SETLOC: =MID
ELSE
LOC:=NULL
[ENDOFIFSTRUCTURE]
STEP6:EXIT

You might also like