Professional Documents
Culture Documents
Lecture-10-11-12: Array
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
3
Representation in Memory
• Two basic representation in memory
– Have a linear relationship between the
elements represented by means of
sequential memory locations
[ Arrays]
6
Linear Arrays
• A linear array is a list of a finite
number of n homogeneous data
elements ( that is data elements of the
same type) such that
– The elements are of the arrays are
referenced respectively by an index set
consisting of n consecutive numbers
– The elements of the arrays are stored
respectively in successive
memory locations
7
Linear Arrays
• The number n of elements is called the
length or size of the array.
• The index set consists of the integer 1,
2, … n
• Length or the number of data elements of
the array can be obtained from the index
set by
Length = UB – LB + 1 where UB is
the largest index called the upper
bound and LB is the smallest index
called the lower bound of the arrays
8
Linear Arrays
• Element of an array A may be denoted
by
– Subscript notation A1, A2, , …. , An
– Parenthesis notation A(1), A(2), …. , A(n)
– Bracket notation A[1], A[2], ….. , A[n]
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]
206
207 LA[4]
Linear Array
•••
16
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••
17
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••
18
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••
19
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••
20
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••
21
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••
1. Repeat for K = LB to UB
Apply PROCESS to LA[K]
[End of Loop]
2. Exit 22
Inserting and Deleting
• Insertion: Adding an element
– Beginning
– Middle
– End
23
1 Brown
Insertion 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
1 Brown 1
Insertion
Brown 1 Brown 1 Brown
2 Davis 2 Davis
3 Ford 3 Ford
4 Johnson 4 Johnson
5 Smith 5 Smith
6 Taylor 6 Taylor
7 Wagner 7
8
8
Deletion of Wagner at the End of Array
26
Deletion
1 Brown 1 Brown 1 Brown
1 Brown
2 Davis 2 2 Ford
2 Ford
3 Ford 3 Ford 3
3 Johnson
4 Johnson 4 Johnson 4 Johnson
4
5 Smith 5 Smith 5 Smith
5 Smith
6 Taylor 6 Taylor 6 Taylor
6 Taylor
7 Wagner 7 Wagner 7 Wagner
7 Wagner
8 8 8
Deletion of Davis from the Array 8
27
1
Deletion
Brown
2 Ford
3 Johnson
4 Smith
5 Taylor
6 Wagner
7
No data item can
8 be deleted from an empty
array
28
Insertion Algorithm
• INSERT (LA, N , K , ITEM) [LA
is a linear array with N elements and K is a
positive integers such that K ≤ N. This algorithm
insert an element ITEM into the Kth position in
LA ]
1. [Initialize Counter] Set J := N
2. Repeat Steps 3 and 4 while J ≥ K
3. [Move the Jth element downward ] Set LA[J
+ 1]
:= LA[J]
4. [Decrease Counter] Set J := J -1
5 [Insert Element] Set LA[K] := ITEM
6. [Reset N] Set N := N +1;
7. Exit 29
Deletion Algorithm
• DELETE (LA, N , K , ITEM) [LA is a
linear array with N elements and K is a positive
integers such that K ≤ N. This algorithm deletes Kth
element from LA ]
1. Set ITEM := LA[K]
2. Repeat for J = K to N -1:
[Move the J + 1st element upward] Set LA[J]
:= LA[J + 1]
3. [Reset the number N of elements] Set N := N -
1;
4. Exit
30
6.8 Searching Arrays: Linear Search and
Binary Search
Search an array for a key value
Linear search
Simple
Compare each element of array with key value
Useful for small and unsorted arrays
31
Searching Arrays: Linear Search and Binary
Search
Binary search
For sorted arrays
Compares middle element with key
If equal, match found
If key < middle, looks in first half of array
If key > middle, looks in last half
5
Repeat
32
Multidimensional Array
• One-Dimensional Array
• Two-Dimensional Array
• Three-Dimensional Array
• Some programming Language allows as
many as 7 dimension
33
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
34
2D Arrays
The elements of a 2-dimensional array a
is shown as below
Jth A[J][K]
Row
38
A5x6 2D Array (Column Major)
Kth
Column
Jth A[J][K]
Row
39
2D Array in Memory
A Subscript A Subscript
(1,1)
(1,1) Row 1
(2,1) Column 1 (1,2)
(1,3)
(3,1)
n (1,4)
(1,2)
2 (2,1)
(2,2) Colum (2,2) 2
mn (2,3) Row
(3,2) 3
(1,3)
(2,4) w
(2,3) Colu n
(3,1) 3
4
(3,2) Ro
(3,3)
Column-Major
(1,4) Order (3,3)Order
Row-Major 41
2D Array
• LOC(LA[K]) = Base(LA) + w(K -1)
• LOC(A[J,K]) of
A[J,K] Column-Major
Order
LOC(A[J,K]) =
Base(A) +
w[m(K-1) + (J-
1)]
Row-Major Order 42
•
2D Array Example
Consider a 25 x 4 array A. Suppose the
Base(A) = 200 and w =4. Suppose the
programming store 2D array using row-
major. Compute LOC(A[12,3])
Column subscript
Array
name Row subscript
44
6.9 Multiple-Subscripted Arrays 1
3
2
4
Initialization
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
Initializers grouped by row in braces
If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 1 0
Referencing elements 3 4
45
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
• Let C be a n-dimensional array
• Length Li of dimension i of C is the
number of elements in the index set
Li = UB – LB + 1
• For a given subscript Ki, the effective
index Ei of Li is the number of indices
preceding Ki in the index set
Ei = Ki – LB
47
Pointer, Pointer Array
• Let DATA be any array
• A variable P is called a pointer if P points to an
element in DATA i.e if P contains the address of
an element in DATA
• An array PTR is called a pointer array if each element
of PTR is a pointer
48
Two Dimensional
4x9 or 9x4 array
Group 1 Group 2 Group 3 Group
4
Troy
Wagner
49
1 Evan
2 Harris
Group 1
3 Lewis
4 Shaw
5 Conrad
Group 2
: :
13 Wagner
Group 3
14 Davis
15 Segal
Group 4
16 Baker
: :
50
1 Evan
: : Group 1
4 Shaw
5 $$$
6 Conrad Group are not index
Group 2 in this
: : representation
14 Wagner
15 $$$ Group 3
16 Davis
17 Segal
18 $$$
19 Baker Group 4
51
1 Evan
2 Harris Group 1
Group
3 Lewis
1 1 4 Shaw
2 5
5 Conrad
3 14
4 16 : : Group 2
5 22 13 Wagner
14 Davis
Group 3
15 Segal
16 Baker
: : Group 4
21
Reed 22
52