Array Datastructure
Array Datastructure
Data Structure
1
Data Structure vs Storage Structure
• Data Structure : The logical or
mathematical model of a particular
organization of data
• Storage Structure :
Representation of a particular data
structure in the memory of a
computer
• There are many possible storage
structure to a particular data
structure
Ex: there are a number of storage
structure for a data structure
such as array
It is possible for two DS to
represented by the same storage
structure 2
Classification
Data Structure
Linear
Non-Linear
A Data structure is
said to be linear if its
elements from a
sequence or in other
words form a linear
list
3
Representation in Memory
4
Operation on Linear Structure
5
Array
6
Linear Arrays
7
Linear Arrays
9
Representation of Linear Array in Memory
1000
1001
1002
1003
1004
1005
:
Computer Memory
10
Representation of Linear Array in Memory
11
Representation of Linear Array in Memory
12
Example 1
200 LA[1]
Find the address for LA[6] 201 LA[2]
Each element of the array
202 LA[3]
occupy 1 byte
203 LA[4]
204 LA[5]
205 LA[6]
206 LA[7]
207 LA[8]
200
Find the address for LA[16] LA[1]
201
Each element of the array
202
occupy 2 byte LA[2]
203
204
LA[3]
205
206
LA[4]
207
15
Traversing Linear Arrays
Linear Array
•••
16
Traversing Linear Arrays
Linear Array
•••
17
Traversing Linear Arrays
Linear Array
•••
18
Traversing Linear Arrays
Linear Array
•••
19
Traversing Linear Arrays
Linear Array
•••
20
Traversing Linear Arrays
Linear Array
•••
21
Traversing Linear Arrays
Linear Array
•••
1. Repeat for K = LB to UB
Apply PROCESS to LA[K]
[End of Loop] 22
2. Exit
Inserting and Deleting
23
Insertion
1 Brown 1 Brown
2 Davis 2 Davis
3 Johnson 3 Johnson
4 Smith 4 Smith
5 Wagner 5 Wagner
6 6 Ford
7 7
8 8
24
Insertion
26
Deletion
27
Deletion
1 Brown
2 Ford
3 Johnson
4 Smith
5 Taylor
6 Wagner
7
8
30
Multidimensional Array
One-Dimensional Array
Two-Dimensional Array
Three-Dimensional Array
Some programming Language allows as many as 7
dimension
31
Two-Dimensional Array
A Two-Dimensional m x n array A
is a collection of m . n data
elements such that each element
is specified by a pair of integer
(such as J, K) called subscript
with property that
1 ≤ J ≤ m and 1 ≤ K ≤ n
32
2D Arrays
The elements of a 2-dimensional array a is shown as below
36
2D Array in Memory
A Subscript A Subscript
(1,1) (1,1)
(2,1) (1,2)
Column 1 Row 1
(3,1) (1,3)
(1,2) (1,4)
(2,2) Column 2 (2,1)
(3,2) (2,2)
(1,3) Row 2
(2,3)
(2,3) Column 3 (2,4)
(3,3) (3,1)
(1,4) (3,2) Row 3
(2,4) Column 4 (3,3)
(3,4) (3,4)
37
Column-Major Order Row-Major Order
2D Array
LOC(LA[K]) = Base(LA) + w(K -1)
LOC(A[J,K]) of A[m,n]
Column-Major Order
LOC(A[J,K]) = Base(A) + w[m(K-1) + (J-1)]
Row-Major Order
LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)]
38
2D Array Example
39
Multidimensional Array
An n-dimensional m1 x m2 x …. X mn array B is a
collection of m1.m2…mn data elements in which each
element is specified by a list of n integers – such as K1,
K2, …., Kn – called subscript with the property that
1≤K1≤m1, 1≤K2≤m2, …. 1≤Kn≤mn
The Element B with subscript K1, K2, …,Kn will be denoted
by
BK1,K2, …,Kn or B[K1,K2,….,Kn]
40
Multidimensional Array
41
Multidimensional Array
42
Example
43
Example Contd ..
44
Pointer, Pointer Array
45
Group 1 Group 2 Group 3 Group 4
Evan Conrad Davis Baker
Harris Felt Segal Cooper
Two Dimensional
Lewis Glass Ford 4x9 or 9x4 array
Shaw Hill Gray
Penn Jones
Silver Reed
Troy
Wagner
King
46
1 Evan
2 Harris
3 Lewis Group 1
4 Shaw
5 Conrad
: :
Group 2
13 Wagner
14 Davis
15 Segal Group 3
16 Baker
: :
Group 4
21 Reed
47
1 Evan
: :
Group 1
4 Shaw
5 $$$
6 Conrad
Group are not index
: :
14 Wagner
Group 2 in this
representation
15 $$$
16 Davis
17 Segal
18 $$$ Group 3
19 Baker
: :
24 Reed
25 $$$
Group 4 48
1 Evan
2 Harris Group 1
Group
3 Lewis
1 1 4 Shaw
2 5
5 Conrad
3 14
: : Group 2
4 16
5 22 13 Wagner
14 Davis
Group 3
15 Segal
16 Baker
: : Group 4
21 Reed
22 $$$ 49