0% found this document useful (0 votes)
28 views25 pages

3 Arrays Part-1

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)
28 views25 pages

3 Arrays Part-1

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
You are on page 1/ 25

Data Structure and Algorithms

CS-201
Lecture # 03
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 form a sequence or in other
words form a linear list

3
Representation in Memory

• Two basic representation in memory


– Have a linear relationship between the
elements represented by means of
sequential memory locations [ Arrays]

– Have the linear relationship between the


elements represented by means of pointer
or links [ Linked List]

4
Operation on Linear Structure

• Traversal : Processing each element in the


list
• Search : Finding the location of the element
with a given value or the record with a given
key
• Insertion: Adding a new element to the list
• Deletion: Removing an element from the list
• Sorting : Arranging the elements in some
type of order
• Merging : Combining two list into a single list

5
Array

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 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
– Depends upon language translator for physical presentation
• 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]

• The number K in A[K] is called subscript or


an index and A[K] is called a subscripted
variable

9
Representation of Linear Array in Memory

1000
1001
1002
1003
1004
1005
:
Computer Memory

10
Representation of Linear Array in Memory

• Let LA be a linear array in the memory of the


computer
• LOC(LA[K]) = address of the element LA[K] of
the array LA

• As we already discussed, the elements of LA are


stored in the successive memory cells, then What
if array is too large?
– Computer does not need to keep track of the address
of every element of LA, but need to track only the
address of the first element of the array denoted by
Base(LA) called the base address of LA
11
Representation of Linear Array in Memory

• LOC(LA[K]) = Base(LA) + w(K – LB)


where w is the number of words per
memory cell of the array LA [w is the size
of the data type]

12
Example 1

200 LA[0]
Find the address for LA[6] LA[1]
201
Each element of the array
occupy 1 byte 202 LA[2]
203 LA[3]
204 LA[4]
205 LA[5]
206 LA[6]
207 LA[7]

LOC(LA[K]) = Base(LA) + w(K – lower bound) :


LOC(LA[6]) = 200 + 1(6 – 0) = 206
13
Example 2

200
Find the address for LA[15] LA[0]
201
Each element of the array
occupy 2 bytes 202
LA[1]
203
204
LA[2]
205
206
LA[3]
207

LOC(LA[K]) = Base(LA) + w(K – lower bound) :


LOC(LA[6]) = 200 + 1(6 – 0) = 206
14
Multidimensional Array

• One-Dimensional Array
• Two-Dimensional Array
• Three-Dimensional Array

15
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
integers (such as J, K) called subscripts
with property that
1 ≤ J ≤ m and 1 ≤ K ≤ n

The element of A with first subscript J and


second subscript K will be denoted by AJ,K
or A[J][K]

16
2D Arrays
The elements of a 2-dimensional array a is
shown as below

a[0][0] a[0][1] a[0][2] a[0][3]


a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
Rows Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3] row 0


a[1][0] a[1][1] a[1][2] a[1][3] row 1
a[2][0] a[2][1] a[2][2] a[2][3] row 2
Columns Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3]


a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]

column 0 column 1 column 2 column 3


2D Array

• Let A be a two-dimensional array m x n


• The array A will be represented in the
memory by a block of m x n sequential
memory location
• Programming language will store array A
either
– Column by Column (Called Column-Major
Order) Ex: Fortran, MATLAB
– Row by Row (Called Row-Major Order) Ex: C,
C++ , Java

20
2D Array in Memory
A Subscript A Subscript
(1,1) (1,1)
(2,1) Column
1 (1,2) Row 1
(3,1) (1,3)
(1,2) (1,4)
(2,2) Colum n 2 (2,1)
(3,2) (2,2) Row
(1,3) 2
(2,3)
(2,3) Colum n 3 (2,4)
(3,3) (3,1)
(1,4) Column (3,2) Row 3
(2,4) 4
(3,3)
(3,4) (3,4)

Column-Major Order Row-Major Order 21


2D Array

• As in One dimensional array


– LOC(LA[K]) = Base(LA) + w(K - LB)

For two-dimensional array


• LOC(A[J,K]) of A[J,K]
Column-Major Order
LOC(A[J,K]) = Base(A) + w[m(K-LB) + (J-LB)]
Row-Major Order
LOC(A[J,K]) = Base(A) + w[n(J-LB) + (K-LB)]

22
2D Array (Row Major)
A5x6 Kth
Column

Jth A[J][K]
Row

LOC(A[J,K]) = Base(A) + w[n(J-LB) + (K-LB)]

23
2D Array (Column Major)
A5x6 Kth
Column

Jth A[J][K]
Row

LOC(A[J,K]) = Base(A) + w[m(K-LB) + (J-LB)]

24
2D Array Example

• Consider a 25 x 4 array A. Suppose the


Base(A) = 200 and w =2. Suppose the
programming stores 2D array using row-
major. Compute LOC(A[12,3])

• LOC(A[J,K]) = Base(A) + w[n(J-LB) + (K-LB)]

• LOC(A[12,3]) = 200 + 2[4(12-1) + (3 -1)]


= 292

25

You might also like