Sie sind auf Seite 1von 26

# Chapter 2 - Arrays

## Dr. Ashwini Rao

Asst. Prof. IT
Types of Data Structures

##  Linear – if its elements form a sequence.

 Ex: Arrays, lists, linked list etc.
 Non linear – elements not in a sequence.
 Ex: Trees , graphs etc.
Linear Data structure

##  2 ways of representation in memory.

 By means of sequential memory locations
– Arrays
Operations on Linear
Structures
 Traversal -processing each element in the list
 Search – finding each element and its position.
 Insertion – adding a new element to the list.
 Deletion – removing an element from the list.
 Sorting – arranging elements of the list in some
order.
 Merging – combining two lists into a single
list.
Linear Arrays
 Is a list of finite number ‘n’ of homogeneous data
elements such that:
 The elements of the array are referenced respectively
by an index set consisting of ‘n’ consecutive numbers.
 The elements of the array are stored respectively in
successive memory locations.
 Elements of the array denoted by subscript notation,
A1,A2,A3… or bracket notation A[1],A[2],A[3]….
 Length of the array is obtained as,
Length=upper bound – lower bound +1
 Allocation: Static (allotment during compilation) or
Dynamic (allotment during execution).
Array Example

1 45
2 34 45 34 89 25
3 89 1 2 3 4
4 25

## Length=upper bound – lower bound +1

Length = 4-1+1=4
Representation in memory
 Elements stored in successive locations.
 Address of the first element to be known and is denoted as
Base (Array_name)
 Address of any other element calculated as:
LOC(Array_name[K])=Base(Array_name)+w(K-Lower bound)

1001 array.

1002
1003

## Fig: Computer Memory

Ex: Representation of Arrays
in memory
200
• BASE(AUTO)=200 and w=4
201 AUTO[1932] • Address of array element for the year
k=1965 is:
202
203 LOC(AUTO[1965])=
Base(AUTO)+w(1965-lower bound)
204 =200+4(1965-1932)=332
205 AUTO[1933]
206
207
208
209 AUTO[1934]
210
211
Traversing Linear Arrays
 Accessing and processing each element of the array
exactly once.
 Algorithm: LA is an linear array with LB being the
lower bound and UB the upper bound.
1. set K=LB
2. repeat steps 3 & 4 while K≤ UB
3. display LA[K]
4. set K=K+1
5. Exit
Inserting & Deleting
 At the end , in the beginning or in-between.
 Requires movement of elements if insertion or deletion
needs to be done in the beginning or in-between the
array.
Insertion Deletion
 Ex:
1 bb 1 bb 1 bb 1 bb
2 dd 2 dd 2 dd 2 ff
3 jj 3 ff 3 ff 3 jj
4 ss 4 jj 4 jj 4 ss
5 ww 5 ss 5 ss 5 tt
6 6 ww 6 tt 6 ww
7 7 7 ww 7
8 8 8 8
Algorithm for Inserting into
an Linear Array
 Algorithm INSERT (LA,N,K,ITEM) : LA is an
linear array with N elements and K is a positive
integer such that K≤ N.
1. Set J=N
2. Repeat steps 3 and 4 while J ≥ K
3. Set LA[J+1]=LA[J]
4. Set J=J-1
5. Set LA[K]=ITEM
6. Set N=N+1
7. Exit
Algorithm for Deleting an
element from the Linear Array
 Algorithm DELETE (LA,N,K,ITEM) : LA is an linear
array with N elements and K is a positive integer such
that K≤ N.
1. Set ITEM=LA[K]
2. Repeat for J =K to N-1
3. Set LA[J]=LA[J+1]
[End Loop]
4. Set N=N-1
5. Exit
Multidimensional Arrays
 Elements referenced by two or more subscripts.
 Two dimensional:
 Also called as matrices/tables/matrix arrays
 Each element is specified by a pair of integers
called subscripts with the property that 1≤ J ≤m
and 1≤K ≤ n
 Element denoted as AJ,K or A[J,K]
 Length of the array is obtained as,
Length=upper bound – lower bound +1
Two Dimensional Array
Example
Columns

Rows

## Fig. Two Dimensional 3×4 Array A

Example - Array Representation

1 2 3 100 11 100 11
1 11 12 13 104 12 104 14
2 14 15 16 108 13 108 17
3 17 18 19 112 14 112 12
2 Dimensional Array, A[3][3] 116 15 116 15
120 16 120 18
124 17 124 13
128 18 128 16
132 19 132 19
Row Major Column Major
Representation of Two
Dimensional Arrays in memory

## (W is the number of words per memory location)

Ex: Row Major Order
Ex: Row Major Order
Ex: Column Major Order
Pointer Arrays
 Pointer: Variable P is a pointer if it points
to an element in data (i.e, it contains the
address of an element in data)
 If each element of the array is a pointer,
its called an Pointer Array.
 Used to facilitate the processing of
information in data.
Ex: Need for Pointer Arrays

## Group 1 Group 2 Group 3 Group 4

Evans Conard Davis Baker
Harris Felt Segal Cooper
Lewis Glass Ford
Shaw Hill Gray
King Jones
Penn Reed
Silver
Wagner

0 0 0 0

0 0 0 0 0 0
0
Ex: Need for Pointer Arrays
Member

1 Evans
2 Harris Group 1
3 Lewis
4 Shaw

5 Conard
-
- Group 2
-
13 Wagner
14 Davis
Group 3
15 Segal
16 Baker
-
- Group 4
-
21 Reed
Ex: Need for Pointer Arrays
Member

1 Evans
2 Harris
3 Lewis
Group 1
4 Shaw

5 \$\$\$\$\$

6 Conard
-
- Group 2
-
14 Wagner
15 \$\$\$\$\$
16 Davis
Group 3
17 Segal
18 \$\$\$\$\$
19 Baker
-
- Group 4
-
24 Reed
Ex: Need for Pointer Arrays
Member

1 Evans
2 Harris
Group 1
3 Lewis
Group 4 Shaw

1 1 5 Conard

2 5 -
-
3 14 - Group 2
4 16
13 Wagner
14 Davis
15 Segal Group 3
16 Baker
-
- Group 4
-
21 Reed
Ex: Need for Pointer Arrays
Member

1 Evans

Group 2 Harris
Group 1
1 1 3 Lewis

4 Shaw
2 7
3 19 5

4 23
6

Number
7 Conard
1 4
. -
2 9 - Group 2
-
3 20 15 Wagner

4 6 16

17
Free
18
1 2 19 Davis

2 3 20 Segal
Group 3
3 2 21

4 4 22

23 Baker

-
- Group 4
-

28 Reed