0 ratings 0% found this document useful (0 votes) 5 views 112 pages Data Structure
The document provides an overview of data structures, including definitions of data items, entities, fields, records, and files, as well as types of data structures such as linear and non-linear. It details operations on data structures, including traversing, inserting, deleting, searching, sorting, and merging, along with algorithmic notation and control structures. Additionally, it explains the representation of linear arrays in memory and algorithms for operations like summing and averaging elements in an array.
AI-enhanced title and description
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Carousel Previous Carousel Next
Save Data structure For Later Data Item : is a single unit of values.
1 Group Items : Data itmes those can divided into sub itmes.
Ex. Full Name : First Name , Middle Name, Last Name
Address iplace, city, district, state, country
Date of Birth : Day, Month, Year
2. Elemenrty Items : Data items those can not be divided into sub items.
Ex. Age: 7
Mobile: 98 9xxxxxxx
Pincode: 442907Anentity :
An entity is something, that has certain attributes or
properties: which assigned values.
Name ; Vinit Sharma Values can be
Age: 17 numeric,
Gender : Male non numeric. or
Class: th alpha numeric
Every attribute has certain value.
The collection of ail entities form an entity set16
Age = 16
The term “information” is also used for data with given attributes.
Information means meanigful data.
Age = -45more attritbutes
] Class :
City :
]
Gender :
] Mobile No.
Feild: Field’ is single elementary unit of
information representing an attribute of entity.Feild: ‘Field’ is single elementary unit of
information representing an attribute of entity.Record : A recordis a collection of field values
of a given entity
File ; A file is a collection of records of
the entities.‘So data can be stored in feilds, records and files.
Apart from fields, records and files, the data can also be organised into
more complex types of structures.
Data can be organised in different ways.
“ The logical or mathematical model of a particular organisation
is called a Data structure.”
yy
Linear Data structure
Non-Linear Data structureis called a Data structure."
Linear Data structure —
Larray Non-Linear Data structure
2 Ponter array LTree
‘1. Record 2 Binary Ty
4 Linked istData Structure operations 3
Array $ Collection of homogenious(same type) elements is called as Array.
(8, 10, 5, 36, 7,32, 4.) without decimal point (Integer Numbers)
{ 1.2, 6.3, 0.5, 9.5, 8.9} with decimal point
Eg. Data
Size / Length= 5
Index Number ——) 1
52 14
2
Datatl\ = 52
Data[2] = 14
Data[3] = 23
Data[4]=7
Data[5] = 27
(Real Numbers)
23 7 27
3 4
4 5Data Structure Operations : there are total six data structure operations.
1. Traversing : Accessing each element of Array(Structure) once so that it can be processed.
2. Inserting : Adding new element into the structure
3. Deleting : — Removing element from the structure
4. Searching : Finding the location of element
5.Sorting: Arranging elements in some logical order { 7, 12, 14, 23, 27, 52]
6.Merging: Combining two files into single file. { 4,8, 3, 10 }
{ 7, 12, 14, 23, 27, 52, 4, 8, 3, 10}gorithimic notation
An algoritm is a finite step by step list of well defined instructions for
solving a particular problem, 7 First Part
Name of the Algorithm
Addition of two int numbers lst of the variables
Let A and Bare two int numbers.
This algorithm will calculate SUM of these two numbers.
Purpose of the Algorithm
Step 1. Start > Second Part
AandB A= 10 B=20 List of steps
3. Calculate SUM = A+B Su = 10+ 20 Steps are executed one after
4. Display SUM 30 the other
Data /values may assigmed to variables
5. Stop.
by using read statement
Stop statment complete the algorithm Display statments displayed result or
value of the variableControl St res
There are three types of flow of control (or logic)
1. Sequential flow (Sequential / linear logic)
2. Conditional flow (Selection logic)
3. Repetitivel flow (Iteration logic)
1. Sequential flow (Sequenti: ar logic)
In sequential flow the modules are executed one after the other
Each modeule is a set of steps
Diagram [ Module A] Eg. 1, Addition of any two numbers
2. Area of circle / triangle / rectangle etc.
[ Module B 3. Average of three numbers
[ Module c ]2. Conditional flow (Selection logic)
In conditional flow, one or other module is selected upon depending on condition.
There are three conditional structures
i) Single alternative (Simple if)
Form / Syntax Diagram
IF (condtions), then :
[MODULE A]
[End of IF structure |
aForm / Syntax Diagram
IF (condtions), then; x
[MODULE A] a voor |
[End of IF structure }
WY
ji) Double alternative (if..else)
Form / Syntax Diagram
IF (condtions), then ;
[MODULE A} Soap woot |
ELSE:
[ MODULE B] ss
[End of IF structure ] Lb
[_woowen ]
Liii) Multiple alternative (Nested if.else)
Form / Syntax
IF (condtion 1), then :
[MODULE A]
ELSE : IF (condition 2), then
MODULE B]
ELSE:
[ MODULE C)
[End of IF structure ]3. Repetitive flow (Iteration logic)
Here certain Module is executed repetdly unity condition satisfies.
i) While loop
Form / Syntax Diagram
Intialize counter
Repeat MODULE A While (condtion) [Lintiatze counter J
[MODULE A]
Increment /Decrememt counter
[End of While]
iromsat conte {te
[croment comer] y
| [Cocoon]li) for loop
Form / Syntax
Repeat for K=R toS By T
[MODULE]
[End of for loop)
Here
K is index varibale. (loop / Counter variable)
Initial value of K is R and
final value is S.
T is increment.
Working and diagram of for loop is same as the while loopRoots of quadratic equation
ax2+ bx + C= 0 where a is not equal toO
X1 and X2 =
2a
Roots and their nature = d = b’b-4*a*c
i) If d< Oi.e. negative :roots are imaginary sqrt(-ve) sqrt(-4) ?
ii) If d == Oi.e. zero: roots are realand equal sqri(O) x1=x2=-b/(2*a)
iii) If d> O1.e. positive : roots are real and different sqrt(4)
+2 +2Roots of quadratic equation:
Let A, Band € are coefficient of quadratic equation and this algorithm will
find real roots of quad. Equation.
1) Start
2) Read A, B,C
3) IF (A == 0) then
i) Display its linear equation
ELSE: IF (D == 0) then
i) Display Roots are Real and equal
li) Calcualte Xi = X2 = -B/ (2* A)
iii) Display X1 and X2
ii) X=-C/B
iii) Display X ELSE
ELSE i) Display Roots are real and different.
ii) Calculate X1 = (-B + sqrt(D)) / (2* A)
i) Caclulate D = B*B-4*A*C iii) Caleulate X2 = (-B - sqrt(D)) / (2 * A)
ii) IF(D ¢ o' then iv) Display X1 and X2
a) Display Roots are Imaginar:
) Display ginary 4) StopTo Display Word "Computer Science" 5 times
1) Start
2) Display "Computer Science”
3) Display "Computer Science”
4) Display "Computer Science"
5) Display "Computer Science"
6) Display “Computer Science"
7) Stop kTo Display Word "Computer Science" 5 times
1) Start
2) Initialize Counter
setK =1 K =1,2,3,4,5
3) Repeat steps ath and 5th While (K <= 5)
4) Display "Computer Science"
5) Increment Counter
K=KetTo Display Word "Computer Science” 5 times
1) Start
2) Initialize Counter
setK=1 K=1,2,3,4,5
3) Repeat steps 4th and 5th While (K <= 5)
4) Display "Computer Science"
5) ee Counter K=K+1
one rk ated
6) Stop =
‘YES
NOTo Display Word "Computer Science" 5 times
1) Start
2) Initialize Counter
setK =1 K = 1,293, 4,5
3) Repeat steps 4th and 5th While (K <= 5)
4) Display "Computer Science”
5) Increment Counter KeKe4
K=Ket is
Ste =
6) Stop r 2
4) Start
2) Repeat step 3rd for K=1toSby1
3) Display "Computer Science"
4) Stop
YES
NO[Arrays ]
1) Its the one of the linear data structure.
2) It is also called as Linear Array
3) It is a collection of finite n numbers of homogeneous data elements.
4) Data elements are of same type.
5) The elements of array ferenced by respectively by an index numbers.
a The elements of array are stored respectively in successive memory locations.
[ 25 is 14 is 2 [ 7 } The total number of elements N is called as length o
anil size of the array
index numbers 2 r[ Arrays |
1) Its the one of the linear data structure.
2) It is also called as Linear Array
3) It Is a collection of finite n numbers of homogeneous data elements.
4) Data elements are of same type.
5) The elements of array are referenced by respectively by an index numbers.
m ‘The elements of array ai tored respectively in successive memory locations.
E 25 ie 4 Is | 7 ] The total number of elements Nis called as length o
a size of the array
inter mame 4 The length of array canbe obatined by using formuls
[Length = ua. tB + 2
=@-1e2 It here LB * Othen UB = 3
Low er Bound UB =3¢+1 #3001
maiiost tadex Mumbe: Upper Bound
Smallost index Highest index Number = 4 “4
®[Arrays |
1) Its the one of the linear data
also called as Linear Array
collection of finite n numbers of homogeneous data elements.
4) Data elements are of same type.
5) The elements of array ai enced by respectively by an index numbers.
= The elements of are stored respectively in successive memory locations.
ie | 14 is 2 L 7 a of array canbe denoted by subscript notaion
wee
Marks, , Marks, , Marks ,, Marks,
ructure.
nde numb:
7 Marks([3), Marks[4)
town us Marks[1| = 25
er Bound
‘Upper Bound iY Mark s/[2) = 14
Smaitont index Number 4g Sere eo umber therk oft} © 2
Marks[4] = 7Representation of linear arrays in memory
1) Elements of inear array are stored in successive memory cells / blocks
2) The number of cells required depends on type of data elements.
*uls[e]* [=|]
t2'3. 4 8
Memory Address
~ 1000
1001
wo2
1003
1004
1005
1006
i
C—™
Memory Location
Fig, Memory representation of arggyRepresentation of linear arrays in memory
1) Bements of inear array are stored in successive memory cels / blocks
2) The number of cels required depends on type of data elements.
3) Comnputer keep track of address of the first element of array.
| 2 | | 26 | 22 | 4) Here LA is inear array and address of first element s denoted by
2
Base (LA) and caled as base address of LA.
5) Using Base address computer caicuates the address of any
elements of LA by using formkia -
LOC ( LA [K}) = Base(LA) + W(K -LB))
304¢6¢S5Representation of linear arrays inmemory
1) Bements of inear array are stored in successive memory cells / blocks
2) The number of cels required depends on type of data elements.
3) Computer keep track of address of the first element of array.
4) Here LA 6s inear array and address of first element is denoted t
Base (LA) and called as base address of LA,
5) Using Bace address computer caicuates the address of any
lerments of LA by using fornia -
LOC (LA[3])=1000+1(3-1) — Wemumber of wort (bytes)
= 000+2 pe memory cot
= 1002
Memory Location
Fig Memory representation of arrayTravesrsing Linear Arrays
Accessing each element of array only once so that it can be processed.
Algorithm : Traversing Linear Array.
Let LA is a Iinear array with lower bound LB and upper bound UB.
This algorithm wil apply PROCESS to each element of LA.
Stops: 4) Start ta [12 ]42[34 [25]
2) Initialize counter 12 3 4e«
set K=LB K=u— ie
3) Repeat Steps 4th and 5th While (K<=UB)]K=4 LAL] 12 OepiyLary
: sera? esl Oe LAI2) 42
4) Visit Element see K=3 ars) 34
Apply PROCESS to LAK} K=4 LA) 25
5) Increment Counter 7 ye K=5
K=K+1
6) StopSum of all the elements of the array
ta [70 |50|80 |10 |
4
12 3
Sum = 70 + 50 + 80 + 10
Sum = 210
Display Sum
Se,
Sum = 70 + 50 + 60 + 10
Sum = 210
Display SumSum of all the elements of the array
“[ | | [J
123 4
SUM =0 +70
SUM = 70 + 50
‘SUM = 120 + 80
SUM = 200+ 10
SUM = 210
SUM = SUM + LA[1] = 7
SUM = SUM + LA[2] K = 2
SUM = SUM +LA[3] K=3
SUM = SUM + LA[4] K = 4Average of all the elements of the array
Let LA iis a linear array of size N with lower bound
and upper bound UB, this algorithm will find 1a[70 | 50|80 {10 | N=4
AVG all the elements of the array. gt 2 34
¥
Steps : 1) Start
2) Initialize SUM = 0 ict ae aa LAK)
3) Initialize Count . .
MtKee see yes SUM= 0+ LALA)
4) Repeat steps Sth & 6th while <= U8)] «9 aah
5) calculate SUM = SUM + LA[K] a SUM = SUM + LAK)
6) Increment Counter oust SUM = 70 + LA[2)
Renee AVG= Kea SUM = 70 +50
7) Display SUM N 4<= 4) 2 YES SUM = 120 + LA(3}
8) St K=5 SUM = 120 + 80
) Stop eaa)te SUM = 200 +LA4)
‘SUM = 200 +10
SUM = 210
kAverage of all the elements of the array
and upper bound UB, the agortn wil fred | EA 70 [50|80 [10 ] N=4
AVG all the elements of the array. gt 2 8 go
v
‘Steps : 1) Start
2) Initialize SUM = 0 SUM =0
3) Initialize Counter sei ‘SUM = SUM + LA[K]
set K=LB (tend) ?YES SUM = 0 + LA[T)
4) Repeat steps Sth & 6th whileK<=U8)| «oo eeu
5) calculate SUM = SUM + LA[K’ 2<= 4)? YES ee
6) increment Counter } K=3 Ss otal + LAK
K=K+1 sum | 3<4)?vEs SUM = 70 + LA[2]
AVG = Ke4 SUM = 70 +50
7) Display SUM N | iceayoves SUM™120 +LA[3)
8) Calculate AVG= 20/4] kes SUM = 120 + 80
AVG = SUM/N AVG = 525 (8<=4)7NO See
9) Display AVG aaa
40) StopInserting a new element into Array N«4
[80 | 40] 50 |70 ]40]
1 2 3 4 5
TEM =40 K=5
Where K <= Nel
LA[S]= 40 = N=4+1=5
| LA[K] = ITEM | | N=N+1Inserting a new element into Array N«4
ts [80 | 10] 40 |70 |
1 2 3 4
TEM =40 K=3
Where K <= Nel
LA3] = 40
| LAIK] = ITEMInserting a new element into Array Neg
[80 [10] 40 |70 |
ea at 2
ITEM = 40 K=3
Where K <= Nel
LA(3] = 40
| LA(K] = ITEMInserting a new element into Array Ne4
[80] 40] 40 |70 |
a a |
ITEM =40 K=3
Where K <= Nel
LA(3] = 40
| LA[K] = ITEM
404
* cas
1
3 4 5Inserting a new element into Array N=4
[80 10/40 70 |
i 2) 3a te
TEM = 40 K=3
Where K <= Nel
| wea
pe sy etInserting a new element into Array J=Ne4
[8o0]10|/50| — ]70)
1 2 3j=4 5
ITEM = 40 K=3
LAS] = 70
LA[S] = LA[4) LAL J #1) = LAL ry
HattInserting a new element into Array N«=4
ws [80 [40/50 |" 70)
1 2 3
4 5
TEM =40 K=3
LA\s] = 70
LA[S] = LA[4) LAL Nei} =LALN)
tatInserting a new element into Array Joneg
uw [8o0]10/50| ]70)
1 2 3J=4 5
TEM =40 K=3
LAS] = 70
LAIS] = LALA] LAL) +1) = LALJ)
Byes? 1¢!Inserting a new element into Array J=Nh4
w[8o]40| —|50]70)
1 2 3J=4 5
TEM = 40 K=3
LA'S] = 70
Lass} = Lagay | Lai y +t) =Laty) |
LAI] =50 Je J-1
=LA3) 232K
TiftInserting a new element into Array J=Nka
[80 |10| ‘60 ]70
1
2 3 J=4
TEM =40 K=3
LAIS] = 70
Laps) = LA(4) | LAU +1) = LAU
LAs) = . [j=s-1]
LAB) | j= As 4}
spigtInserting a new element into Array J=Ne4
[80] 10/40 | 5070)
2 3 J=4
ame ® K=3 fee ITEM = (40;
LAs] = 70
LAIS] = Lala] [Lays +1) = LAL) |
LAI4] = 50 j=)-1]
=LAB) | j=3-K]
tHAftInserting a new element into Array
INSERT (LA.NITEM K)
Let LA is a inear array of size N, this algorithm wil
insert ITEM at the Kth position of LA, WhereK= K) ing 50 Tee mt
4) Move Ith element downword =LAB) j=3=k)
LAUT = LAU) “—
5) Decrement Counter
JeJ-1
6) Insert ITEM
AK] = ITEM
7)ResetN=N+1
8) StopDeleting a element from Array N=4
ts [80[10|50 [70]
1 2 3 4
K=4 ITEM=70 [TEM=LA/4]
[Tem = LIK) JDeleting a element from Array N=4
ts [80[10/50
1 2 3
K24 ITEM =70_ ITEM=LAlA
ITEM = LAIK]Deleting a element from Array N=4
ts [80 [10[50 |n ewes
1 2 3
K=4 {TEM=70 _(TEM=LA(4)
ITEM = LAK]Deleting a element from Array N=4
ts [80[10]50 [70 ]
1 2 3 4
K=2 ITEM=10 ITEM=LA(2
ITEM = LA[K]Deleting a element from Array N=4
w[so{ [50 |70]
1 23 4
K=2 ITEM = 10 _ITEM=LA\2]
r [ITEM = LAIK] |Deleting a element from Array N=4
is[80] |50|70]
1 2 3 4
K=2 (TEM =10 _ITEM=LA(2]
[eM = LAKDeleting a element from Array N=4
80| |50|70]
2 03 «4
1
K
2 ITEM = 10 _ITEM=LA\2]
ITEM = LAIK]
pe ttDeleting a element from Array N=4
[so] |50/70]
1 2 3 4
K=2 ITEM=10 _ITEM=LA(2j
[em = Lak) |
|
>
1
ttDeleting a element from Array N=4
w[8o| |50{70]
1 2 3 4
K=2 ITEM=10 ITEM= LA/2)
[Tem = Lai! J
‘ff
[—
3 4
hDeleting a element from Array N=4
[so] [50 [70]
1 2 3 4
K=2 MEM=10 _(TEM=LA(j
ITEM = LAIK]Deleting a element from Array N=4
[8o[50| |70]
1 Je2 3 4
| J=Kl=2 ITEM =10 _ITEM=LA(2]
ITEM = LA(K] |
Lal2]=50
LA2] = LAI3)
LAU] = LAL + 1]
Set
1 2Deleting a element from Array
[80|50|70| |]
1 J=2 2 4
J=kl=2 ITEM = 10 _ITE
ITE
LA[2] = 50
LA] = LAB LAB aTe
_ (tasuden) PARISDeleting a element from Array Ne=4
80 |50 | 70 ]
1 Ja2 3 4
| J=K|= 2 ITEM =10 _ITEM=LA(2)
ITEM = LAK] ]
LA[3] = 70
LA(3] = te
J=J+1)[J=3=N-1
LA(2] = 50
LAI2] = LAI)
_ [a= Laue tl} |DELETE(LA, N, ITEM, K)
Let LA is a linear array of size N, this algorithm |,
will delete element ITEM from Kth position.
where K is any positive integer such that K <=N
‘Steps : 1) Start
2) set ITEM =LA[K ]
3) Initialize counter
set J=K
4) Repeat Sth & 6th step while J<=N-1
5) Move J+1 th element upword
LA[J] = LA[J+1]
6) Increment Counter
Jaut1
7) Reset N=N-1
8) Stop
N=3
[80 [50 |70
1 2 3
K= 2 ITEM=10
J=k
J=2 J<=N-1 2<23 YES
LAfJ] = LA[J+1]
LA[2] = La[2+1)
LAI2] = LA[3]
LAI2] = 50
J=3 J<=N-1 3<=3 yES
LA[J] = LA[J+1]
LA[3] = LA[4]
LAI3] = 70
J=4 J<=N-1 4<=3 NO
————OE~_—Algorithm : Searching
Finding the location of element in array is called as searching.
1} Linear search DATA ee ieaisl
LOC S14 -2"\.3
N=5
CTEM = 15 Loc=3
siniiai
is 6 == 12? NO
F (15 == 12)
Loc =1 | F(meM==paTai)}
LOC =2. | FIITEM==DATA(2})]
F( 1% == 39) NO
ITEMis found atLOC. LOC =3 | F(ITEM==DATA({3)]}]
. F(15==15) YESLINEAR( DATA, N, ITEM, LOC )
Let DATA is a linear array of size N. N=5
Wacerchisuneuccessiult wilsat LOC MLL DATA | alse] 6 | 26 [36]
1 3.4 5
Steps : 1) Start ec*
2) Initialize LOC (TeM=39 LOC=?
Set LOC =14
3) Repeat steps 4th & Sth while LOC <= N is (LOC <=N) ?
4) Compare ITEM 1 N) then eee rie
Display ITEM Is not foundLINEAR( DATA, N, ITEM, LOC )
Let DATA is a linear array of size N. N=5
this algorithm wal find the LOC of ITEM in DATA and
if search s unsuccessfulit wilset LOG =NULL ATA | 12| 39] 45 | 26 |35]
- orl ¥ 3 45
mi bialze LOC TeM=85 LOC=?
=
3) Repeat steps 4th & Sth While LOC <= N Sees
4) Compare ITEM LOC =2 30 NO”
IF( ITEM == DATA [LOC] ), then LOC =3 85==15 NO
i) Display ITEM is found at Li LOC =4 85 ==26 NO
7 eo LOC =5 85 ==35 NO
LOC =LOC +4 are a NO
6) F (LOC > N), then
Display ITEM is not found
7) StopAlgorithm : Searching
finding the location of element in the array is called searching
2) Binary search Im this search technique array (DATA) should be sorted.
vara] 40 [20|30]40|[50| 60/70/80] N=8
wesre? 7 * * 3 © opie” ITEM= 40 LOC =?
MID = INT(BEG + END) / 2
MID = INT( 1+8) /2
MID = INT 9/2
MID = INT(4.5)
MID= 4Algorithm: Searching
finding the location of element in the array is called searching
2) Binary search in this search technique array (DATA) should be sorted.
paral 40 20 [30 | 40 50 me a) N=8
bec -\8?" . Bon Fe ee? ITEM=40 LOC =?
MID = INT(BEG + END) / 2
MID =INT( 1+8)/2
MID = INT 9/2
MID = INT(4.5)
MID = 4Algorithm : Searching
finding the location of element in the array is called searching
2) Binary search Im this search technique array (DATA) should be sorted.
cara 10 [20] 30 [/40\[50 | 6] 70[80] N=8
12_3 4 ITEM=40 LOC =?
‘Ast Half 2nd Half
1
BEG =\8*
MID = |NT(BEG+ END) /2 — St Half (10, 20,30) < 40 DATA (MID)
MID = INT( 1+ 8) /2 2nd Half (50, 60, 70,80) > 40 DATA [MID]
MID = INT 9/2
MID = INT(4.5)
MID= 4
DATA(MID] = 40Algorithm : Searching
finding the location of element in the array is called searching
2) Binary search i this search technique array (DATA) should be sorted.
oara[ 40 | 20] 30 [[40][50] 60[70[80] N=8
eeca\s*” = 3 ot 5 Saw ® ITEM= 40 LOC =?
MID = INT (BEG + END) / 2 4st Half (10,20,30) < 40 DATA [MID]
MID = INT( 1#8)/2 2nd Half (50,60, 70,80) > 40 DATA (MID)
=INT9/2 Compare
MID = INT(4.5) {| F(iTem == ataimm)) }
MD = 4 IF( 40 == DATA[4])
- IF( 40 == 40) YES
DATA[MID] = 40 ITEM is found at LOC = MIDparal 10 | 20| 30 50 | 60|70|[80] N=8
2 $__8 Lye? S erirgad item =20 Loc =?
2nd Half END =
2 3 \a
BEG =\8° ea WO
Mid = INT (BEG + END
MID = INT( 1+8) /2
MID = INT 9/2
MID = INT(4.5)
MID = 4
BALAMID] = 40
4st Half (10, 20,30) < 40 DATA [MID]
2nd Half (50, 60, 70,80) > 40 DATA [MID]
Compare
IF( ITEM == DATA[MID] )
IF( 20 == DATA[4] )
IF( 20 = 40) NOnual 10 | 20] 30 [(40)[50 | 60 [70 [80] N=8
8
secs?! aaa 3 ot 5 hese ITEM = 20 Loc =
2 Cast Half Di, 20, 30) < 40 DATA [MID]
2nd Half (50, 60, 70,80) > 40 DATA (MID)
MID = INT (BEG + END
MID T(1+8)/2
MID = INT 9/2 Compare
= INT(45) IF ITEM == DATA[MID] )
MID= 4 IF( 20 == DATA(4]) IF( 20 == DATA[2] )
IF( 20 == 40) NO IF( 20 == 20) YES
‘A[MID] = 40
1 | IF( ITEM < DATA[MID] ) ] ITEM is found at LOC = MID
=(1+3)/2 1F(20<40) YES SETEND=MID-1 4-1 =3
MID= 4/2
MID = 2 NSparal 40. 20| 30 [/40\| 50 | 60 70[80] N=8
o1__2 3 \4/ 5 6 7 8 ITEM = 60 Loc =:
= —_—_—= ~~ Nb We a u th
BEG =\3° tna 2nd Half END
2 GstHalfDo, 20,30) < 40 DATA [MID]
Mid = it (BEG + END
MID = INT( 1+ 8) /2 2nd Half)50, 60, 70, 80) > 40 DATA [MID]
ID = INT 9 / 2 Compare“
INT(4.5) IF( ITEM > eT
yao IF(60 == DATA[4])
AlMiD] = 40 160 s540)
IFITEM < DATA[MID]) |
(60<40) NO
| FU ITEM > DATA[MID]) |
IF( 60> 40) YESven] [20] 30 [|| 50] 60 [70]0] w=2
teem ITEM = 60 Loc =?
BEG =\8° wi 2nd Half END = V8
4st Half 10, 20,30) < 40 DATA [MID]
Cand 50, 60, 70,80) > 40 DATA [MID]
Compare“
IF( ITEM = Marae )
1F(60 == DATA[4])
(60 == 40) NO
EM < DATA[MID]) |
MID = INT(5+8)/2 (60< 40) NO
Sn mete ce) | F( ITEM > DATA[MID} ) J
al ae (60> 40) YES SetBEG=MID+1 4+4 =5
MID = INT (BEG + END
MID = INT( 1+8) /2
MID =INT9/2
D = INT(4.5)wos Tens esse [me og Lae ss om LOC =?
1
.
BEG = 8 ce Half # amar ND SVS
2 Gast Half D0, 20, 30) < 40 DATA {MID}
C2nd Halt)50, 60, 70,80) > 40 DATA [MID]
Compare WA
MID = INT(4.5) 1F( ITEM. Ge [MiD] ww
oie IF(60 == DATA[4] ) IF( 60 == DATA(6] )
. (60 == 40) NO IF{ 60 == 60)YES
EM < DATA[MID]) } "is found at Loc = Mio
MID = INT(5+8)/2 (60<40) NO
adalah L | IF( ITEM > DATA[MID} ) |
MID = INT (6.5) ‘
a ae (60> 40) YES SetBEG=MID+1 4+4 =5BINARY(DATA, N, ITEM, LOC)
Let DATA is sorted linear array with size N, with lower bound LB and upper bound UB.
BEG denotes begining and END denotes ending and MID denotes middle segment of the array.
This algorithm will find LOC of ITEM in DATA and if search is unsuccesfull
then it will set LOC = NULL
Steps ; 1) start
2) initialize BEG = LB and END = UB
3) Repeat step 4th & 5th while BEG <= END
4) Calculate MID = (INT) (BEG + END) / 2
5) Compare ITEM
IF(ITEM == DATA [MID] )
i) Display ITEM is found at LOC = MID
ii) go to step (stop)
N=3
vara[ 10 | go | 30]
4 2.008
ITEM=20 LOC =?
BEG=1 §END=3
is(BEG <= END) ?
(1<=3)? YES
MID=(1+3)/2 = 4/2 =2
IF( ITEM == DATA(2})
IF( 20 == 20) YESBINARY (DATA, NN, ITEM, LOC)
Let DATA is sorted linear array with size N, with lower bound LB and upper bound UB.
BEG denotes begining and END denotes ending and MID denotes middie segment of the array.
This algorithm will find LOC of ITEM in DATA and if search is unsuccesfull N=3
then it will set LOC = NULL
vata| 10 | 20 | 30]
Steps : 1) start VA
) Initialize BEG = LB and END = UB) 1 28
) Repeat step 4th & Sth while BEG <= ay SE eee
4) Calculate MID = (INT) (BEG + END) / 2 is(BEG <= END) ?
5) Compare ITEM |“ (1<=4)? YES
IF(ITEM == DATA [MID] ) a MID=(1+1)/2 = 2/2 =4
i) Display ITEM is found at LOC = MID IF( ITEM == DATA)
gotostep(stop) hk IF 10 == 10) YES
ELSE IF(ITEM < DATA[MID] )
Set END=MID-4BINARY (DATA, N, ITEM, LOC)
Let DATA is sorted linear array with size N, with lower bound LB and upper bound UB.
BEG denotes: begining and END denotes ending and MID denotes middle segment of the array.
This algorithm will find LOC of ITEM in DATA and if search is unsuccesfull
then it will set LOC = NULL
Steps ; 1) start
A Initialize BEG = LB and END = UB’
) Repeat step 4th & 5th while BEG <=
4) Calculate MID = (INT) (BEG + END) / 2
5) Compare ITEM |“
(ITEM == DATA [MID] )
Al Display ITEM is found at LOC = MID
lf go to step (stop)
SE IF(ITEM < DATA[MID] )
Set END = MID-1
ELSE Set BEG = MID+ 1
4
VA
N=3
pata 10 | 20 [0]
1 2 \y
JEM= 30 Loc=?
BEG=1 END=3
is(BEG END)
i) Display ITEM is NOT found, Set LOC = NULL
MID=(14+3)/2 = 4/2 =2
IF( ITEM == DATA[2))
IF 30 == 20) NO
ELSE IF(30< 20) NO
ELSE Set BEG= 2+ 1=3
BEG = END = MID = 3
IF 30 == DATA(3})Difference between Linear search and Binary search
Linear search
Binary search
4) In linear search array (data) may or
may not be sorted.
ie. it is not neccessary that data should
be sorted.
4) in binary search array (data) should be
sorted,
2) Item to be search is compared with
every element of array starting from first
location,
2) Item to be search is compared with middle
value of array.
3) Its less efficient than binary search
3) Its more efficient than linear search
4) Time complexity of linear search is O(n)
4) Time complexity of binary search is
Ollog 4n)
5) ExampleAlgorithm : Sorting
arranging elements in some logical order is called
Bubble sort:
Eg. Reration/ pai
DATA NeGKeL PROS DATA NeS Ke2 Pree DATA N#=5 Ke 3 PIRe?
1.33 22 22 22 22 2 2 2
2.22 33-33 33 33 33-533 33
3.44) 44-44-44 44 4-4-1
prme*4.55) 55 55°" 55-41 mu oles
5 (ay) 11 11 11-88 55 55 «65
DATAIPTR) DATA PTR +a) e
DATA (3) DATA (2) K=12,3,4
so om K=1to4
\k =iton- 4
DATA (PTR) > DATA (PTR + 1) ) ves
Interchange their values
PTR © PTR oDAlgorithm : Sorting
arranging elements in some logical order is called as sorting.
Bubble sort :
Eg. Reration pas:
DATA N=SK=i pm DATA N=S K=2 PIR=3) DATAN=5 K=3 Pim=2
aGG3)22 22 2 aGz} 22 22 22 1 (22) 22
2fz2}—3333 33 2[33p/ 33-33 330 pm-2 [33}~39.
PETiMe eet) pm= 3044) 4446-11 3iaa)
om-4/55| 85 55-55 af) 1a dias 44a) 44
Sia) aa aa as 555 55 55 55 5 ss 65
DATAIPTR| DATA (PTR +4) re eke
DATA [| DATA(2) Koh 8,06 sn neee
33 22 [ksitea 7 om-1 22)—48
site fiat
ta DATA [PTR|> DATA (PTR + 1)) | Yes | ral S
[pre ser | Interchange their a & | ah “4
535) 55BUBBLE (DATA, N)
Let DATA is a linear array of size N and this algorithm will sort DATA in accending order.
Steps : 1) Start DATA N=S Kei.2.3.4
2) Repeat steps 3rd and 4th for K = 1toN- 1by 1 1[33}22 22 22 22
3) Set PTR=1 2 [22}/33-33 33 33
4) Repeat and 6th while PTR <= Nk a owe
qcopatacae * a ee
pres [ia] 41 11 11-~ss
IF(DATA[PTR] > DATA[PTR + 1) then, —
Interchange their values is (PTR <=N-K)?
i) Set TEMP = DATA[PTR] TMP = 33 ig ig cS. 4)?
i) Set DATA[PTR] = DATA[PTR+1] OATAIN=22 js (5 <= 4)? NO
oe SRTArRCs= ver PATARI“33 eDATAIPTR] > DATAIPTR + 1)
6) Increment counter F(DATA[4) > DATAIS))
pre =PTR +4 (55> 19) YES
7) Display Sorted DATABUBBLE (DATA, N)
Let DATA is a linear array of size N and this algorithm will sort DATA in accending order.
‘Steps : 1) Start DATA NeGKw1 2.3.4
2) Repeat steps 3rd and 4th for K = 1toN-1by 1 1 [G3}22 22 22 22
3) Set PTR=4 2 (aah 33-83 33 33
aiepeatvep snandemeaermeanck 3 EES
5) Compare elements water
I(DATAIPTR] > DATAIPTR + 1) then, viele i aelhegliaallibad
Interchange thelr values is (PTR <= N-K)?
i) Set TEMP = DATA[PTR) TEMP*33 ig (g ce. 4)?
ii) Set DATA[PTR] =DATA[PTR+1] OATAIR"22 is (g ce 4) ? NO
i) Set DATA|PTR+1) = TEMP DATAIZ +33 IF(DATA[PTR] > DATAIPTR + 1])
ona IF(DATA[4] > DATA[S))
= | 1)
7) Display Sorted DATA ee
8) Stop
iPointer Array:
In array if each elements are pointer then its called as pointer array.
Pointer is a variable which contain the address of the elements.
Pointer points to an element in list.
Let we have four groups.
Each group consist of list of members.
Membership list is to be stored in memory.
The most efficient method is to form two [s |
arrays. One is members consisting of
all members one after the other. Ls |
and another pointer array group contaning |_10_|
the starting locatons of different groups. [ 12 |
Observe that the elements of pointer array
are starting addresses of each group
Xil Class : Students
(Members)
MB=4
cs=3
rT =2
ELE=2Pointer Array :
In array if each elements are pointer then its called as pointer array.
Pointer is a variable which contain the address of the elements.
Pointer points to an element in list.
_- ooo n—25 _]
c001H eo / coosH| 58 |
{ PTR1, PTR2, PTR3...} Addresses fig. Memory locations
Pointer ArrayPointer Array :
In array if each elements are pointer then its called as pointer array.
Pointer is a variable which contain the address of the elements.
Pointer points to an element in list.
a :
cooo Zool 24 ]2
ao = index
pret conan 67 ]3 mnbers
coo1n | -- 7 con] 58 |
pra coal 38 5
{ PTR14, PTR2, PTR3... } Addresses fig. Memory locations
Pointer ArrayRecords : Its a collection of fields.
Record may contain non-homogeneous data.Records : Its a collection of fields.
Record may contain non-homogeneous data.
ie. data items in record may have different Eg.
data types. 4. Student
The elements in record are described by 2. Roll No,
level numbers. 2. ra ane
i First Name //
in record natural odering of elements is not possible 3 hocee thonal
The number to left of each variable is called level number. 3.Last Name
Each group item is followed by its subitem. 2. Gender
The level of subitem is 1 more than the level of group item. 20
‘Any particular field in record can be accessed by 3. Benth
period{dot) Student Full Name.Last Name 2 ater
4. Student(15) It indicates 15 records ina file.
Any particular field in file can be accessed by
Student.Age(5]
»Representation of records in memory
Eg. +.student
2. Roll No. Records contain non homogeneous data.
2. Full Name To store record in memory we need parallel linear arrays.
3. First Name for this example of student record, we requires 5 linear
3. Last Name arrays such as Roll No., First Name, Last Name, Age
2.Age and Class
_<_— one array for each elements of data item.
K __RollNo FirstName Last Name Age ClassRepresentation of records in memory
Eg. 4. Student
2. Roll No. Records contain non homogeneous data.
2. Full Name To store record in memory we need parallel linear arrays.
3. First Name for this example of student record, we requires 5 linear
3. Last Name arrays such as Roll No., First Name, Last Name, Age
2.Age and Class
2. Class
‘one array for each elements of data item.
Roll Nok], First Name[K], Last Name{k], Age{K], Class[K]
h must be belongs to same record
RollNo FirstName _Last Name Age ClassLinked list ; Its a linear collection of data elements, called nodes.
linear order is given by means of pointer.
each node is divided into two parts, first part contains the
information part of the elements and second part contains
the address of the next node in the list
Information part
\\ Pointer of next node
/ (Link part)
EPO EHE RHEE HE BH
Start ‘fame
(Head)
L-Linked list ; Its a linear collection of data elements, called nodes.
linear order is given by means of pointer.
each node is divided into two parts, first part contains the
information part of the elements and second part contains
the address of the next node in the list
Information part
Pointer of next node
/ (Link part )
is [eH se [see le Ti]
Fig. Linked list al 6 Sie.
NULL te
Start /Hame
(Head)Linked list : Its a linear collection of data elements, called nodes.
linear order is given by means of pointer.
each node is divided into two parts, first part contains the
information part of the elements and second part contains
the address of the next node in the list
\ per 7 oe
v ¥ =
‘PHEEHE RHE [eH HE fk
8 4 2 6 3
Fig. Linked list with 6 nodes,
The left part represents the information part of node.
The right part represent pointer field of node.
The arrow is drawn from this field to next node.
pointer field of last node conatin null pointer.
The address of first node in list called as start or name.
We need only this address to trace the linked list.Representation of linked list in memory (aL pot
er
* FeCl He [2 HE Ti)
[a
3
START
Linked list can be represented by two parallel linear arrays.
‘One is INFO containing information part
and other is LINK containing nex t pointer field.
The name of link ed list, start, contains beginnings of the list.
STARTRepresentation of linked list in memory
. ae alge] Atle bh Ts
Linked list can be represented by two parallel linear arrays.
‘One is INFO containing information part
and other is LINK containing next pointer field.
The name of linked list, start, contains beginnings of the list.
START points to 5th element
INFO[5] = A and LINK[S]=8 so next node address is 8
INFO[8] = B and LINK[8]= 4 so next node address is 4
INFO[4] = C and LINK(4]=2 so next node address is 2
INFO[2] = D and LINK[2]=6 so next node address is 6
INFO[6] = E and LINK[6]=3 so next node address is 3
INFO[3] = F and LINK(3] = NULL The list is ended here.Tree sts a non linear data structures.
It used to represent data containing hierachical relationship between elements.
> Root ®) Root / College
_
ay | Ne
ay 8
Arts ©) Commerce
Science
A Binary tree Tis defined as a finit 7 of elements, called nodes, such that -
a) Tis empty ( null tree
b) T contains a distiguished node R, called root of T and the remaining nodes of T
form an ordered pair of disjoint binary trees T1 and T2.
7 Root
Eg. T R Here T conatins root R and T4 and T2 are subtrees of R
_/ \_ lf 11 is non empty, then its root is called as left
1 {2 successor of R and if T2is non empty, then its
~ root is called as right successor of R.a) Tis empty ( null tree or empty tree ) or
b) T contains a distiguished node R, called root of T and the remaining nodes of T
form an ordered pair of sjoint bin 'y trees T1 and 12.
7p) Roo!
Eg. T R) Hi
conatins root R and Ti and ¥2 are subti
a non empty, then its root Is cal
ay (T2) sue T2is non empty,
left - a right root is Successor of
Any node in binary tree has 0, 1 or 2 successor.
The nodes with no successor (child) are called as termin:
Pg Pia nodes, It is also called as leaf.
\) Root : The node which has no parent.
T r Sibling : children of the same parent.
~ The two binary tree are said to be similar if they have
T: Binary tree same stucture.
Y RootAlgebriac ex pression and binary tree
the algebraic expression can be represented by binary tree.
Eg.
E=(@B/(Cer a Fe) Sparc nmer eh se
i os *
3 Np ae ONS
or wo[(a+b)*e]/[a*((b-¢)+a)]
oor
Ooa+2b
at2*bC=(X+Y)°-(5A+B')
C=(X+Y)43-(5*A+B42)Binary tree : parent and child
Suppose Nis node in T with left successor S1 and right successor s2,
then Nis called as parent (father) of s1 and s2.
T parent
The line drawn from node N toa successor is called an edge.
Sequence of consecutive edges is called a path.
et child N-$2-A-D
Gi ($2, Terminal node is leaf and path ending in a leaf is called a branch
— av The depth (height) of a tree Tis the maximum number of nodes:
aa (B) ina branch of T.
left childThe Tis said to be complete / symmetric if allits levels except the last,
have maximum number of possible nodes.
(RY bevero Depth (n) = 3
Cc a) leveld ifn=4 totalnodes=2
oo, B ifm=2 totalnodes=3
Level? QO © F ifn=3 totalnodes=7
= ne?
ar
ifn=4 totalnodes = 15
7
T: 2tree / extended binary tree
Max Nodes =2"-4
ifm=5 totalnodes = 31
The tree T is said to be a 2 tree or an extended binary tree if each node N has
either 0 or 2 childeren.
the node with 2 children are called are internal nodes.
the node with 0 children are called are external nodes.4) Linked representation
Eg Let T be a binary tree root
re [51/2]
~ ]
“ ®) Fe [xiB]x]
7 [xDix] (x[E[x] —_—
The linked representation uses three parallel arrays,
INFO, LEFT, RIGHT and pointer variable ROOT.
Each node N of T will corresponds to a location K such that:
4. INFO[K] contains the data at node N
2. LEFT[K] contains the location of left child of node N.
3. RIGHT[K] contains the location of right child of node N.1) Linked representation
Eg, Let T be a binary tree
[e|¢l4]
5
. [XBix] —[x/E[x] area
6 4
The linked representation uses three parallel arrays,
INFO, LEFT, RIGHT and pointer varigble ROOT.
Each node N of T will corresponds to a location K such that:
1. INFO[K] contains the data at node N
2. LEFT[K] contains the location of left child of node N.
3, RIGHT[K] contains the location of right child of node N.
4. ROOT will contain location of root R of T.1) Linked representation
root
5|Al2
(ig)
(sigia) ——(xiBIx)
5 2
[xD|x} [x] Ex] X= NULL
6 4
The linked representation uses three parallel arrays,
INFO, LEFT, RIGHT and pointer variable ROOT.
Each node N of T will corresponds to a location K such that:
4. INFO[K] contains the data at nocle N i
2. LEFT[K] contains the location of left child of node N.
3. RIGHT[K] contains the location of right child of node N.
4, ROOT will contain location of root R of T.
Linked representation of binary tree2) Sequential representation
Eg. Let Tbe a binary tree
In sequential representaion a linear array is used.
The root A is stored in TREE[1].
If anode N occupies TREE[K], then —
its left child is stored in TREE[2 * K] and
its right childis stored in TREE[2*K+ 1) © TREEIZ] Kea
(
NAQRWN=2) Sequential representation
Eg. Let T be a binary tree
TREE
root
7. 1Ta]
ZK oO ae]
oo OG 38
= 4
In sequential representaion a linear array is used. : |
The root A is stored in TREE[1}. 7 -_]
If anode IN occupies TREE[K], then
its left child is stored in TREE[2 * K] and
its right child is stored in TREE[2*K+4] © TREE[1] Kei
Left Child C ; TREE[2 * K|
TREE(2* 1) TREE (2)
Right Child B : TREE[2 * K + 1]
TREE[3]2) Sequential representation
Eg. Let T be a binary tree
: root(@) demas
1LA |
atc |
3{_B |
4{_p |
, onak . 5 E |
In sequential representaion a linear array is used. 6p *_|
The root Ais stored in TREE[1].
7L
If anode N occupies TREE[K], then
its left chid is stored in TREE[2 * K] and
its right child is stored in TREE[2 * K + 1] C TREE[2] K=2
Left Child D : TREE[2 * K]
TREE[2* 2] TREE [4]
Right Child E : TREE[2 * K + 4]
TREE([5]Stack and Queue:
fa [™ [a7]
1 2 3 4
Item = 56
K=123,4,5
item can be inserted at first position, last position as well as at middle position.
Item = 14
K=2,1,3,4
item can be deleted from first position, last position as well as from middle position.
If we want to restrict inseration and deletion operations,
so that they can take place only at beginnig or the end of list, not in the middle.
Then data structure stack and queue art useful in such situation.Stack :
A stack is a data structure in which items may be PUSH POP
added or removed only at one end. \ /
the item can removed or added only from TP! Front
the top of the stack, [46 _| Last item added = 46
‘st item to be deleted = 4
"PUSH" is the term used to insert an element into stack | 14
"POP" is the term used to delete an element from stack __35_|
A stack is also called as LIFO.( Last In First Out )
a Bottom / Rear
jueue :
rStack :
A stack is a data structure in which items may be PUSH PoP
added or removed only at one end. Top !Front| ¥ /
the item can removed or added only from iP
the top of the stack. [46 _| Last tem added = 46
first item to be deleted = 46
"PUSH" is the term used to insert an element into stack [| 14 |
“POP" is the term used to delete an element from stack
A stack is also called as LIFO. ( Last In First Out )
Bottom / Rear
Queue :
‘A queue is a linear list in which items may be added por PUSH
only at one end and items may be removed only at 27 | 35 | 14 | 46 /
other end. r 2 3 4
The queue is also called as FIFO. ( First In First Out ) Front Rear
r