Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

ARRAYS

Topics to be covered
• Definition of Array
• Types of Arrays
• Operations on 1-D Array
 Creation
 Traversal
 Search
 Insertion
 Deletion
 Sorting
 Merging
• Memory Representation of 2-D Array
 Row – major order
• Sparse Matrix
Arrays
• It is a collection of similar data elements present in
continuous memory location and referred by a unique name.
• Advantages of Arrays: ·
– It is capable of storing many elements at a time ·
– It allows random accessing of elements i.e. any element of
the array can be randomly accessed using index.
• Disadvantages of Arrays:
– The array is static, which means its size is always fixed.
– The memory which is allocated to it cannot be increased or
decreased. It may lead to wastage or deficit in the
memory.
Types of Arrays
• Arrays can be categorized to into two types:
• Single Dimensional Array
– one-dimensional array store single list of elements of
similar data.
– Size of one-dimensional (1D) array is:
• Total Bytes = sizeof (data type of array variable)* size of array.
• Two-Dimensional Array
– Two-dimensional array stores list of lists or array of arrays.
– Size of two-dimensional(2D) array is:
• Total Bytes= sizeof (datatype of array variable)* size of first index*size of second
index.
Single-Dimensional Array
• Let us consider a single-dimensional array A of size 6 i.e. A[6]

• To find address of an element with index “i” :


– Address of A[i] = B + W(K – LB)
– Where:
• B = Base Address
• W = Width or Size of each element
• K = Index of the element to be searched
• LB = Lower Bound of the Array
Single-Dimensional Array

Example on Finding Address of an element in 1-D Array:

Q) Find out address of A[3] in an array of integers. Given base


address is 100.
Ans)
Given, K = 3, B = 100, W = 4 (i.e size of integer)
– Formula is : Address of A[i] = B + W * (K – LB)
– Address of (A[3]) = 100 + 4 * (3 – 0 )
= 100 + 12
= 112
OPERATIONS ON ARRAY(1-D)
CREATION
Creation of an Array: An array will be created with certain number of elements.

Algorithm :
Create[LA,LB,UB,K] :
[ LA => Linear Array, LB => Lower Bound, UB => Upper Bound]
Step 1: Start
Step 2: Let LA[SIZE], LB,UB,K
Step 3: Set LB:=0, UB:=SIZE – 1
Step 4: Display “Enter elements into array”
Step 5: Repeat for K:=LB to UB incremented by 1
Step 5.1: Read LA[K]
[End of For – Step 5 ]
Step 6: Stop
TRAVERSAL
Traversing an Array: We will traverse ( or visit) each element of the array.

Algorithm :
Traverse[LA,LB,UB,K] :
[ LA => Linear Array, LB => Lower Bound, UB => Upper Bound]
Step 1: Start
Step 2: Set LB:=0, UB:=SIZE – 1
Step 3: Display “Elements of the array are:”
Step 4: Repeat for K:=LB to UB incremented by 1
Step 4.1: Display LA[K]
[End of For – Step 4 ]
Step 5: Stop
SEARCHING
Searching an element: Searching refers to finding position of an element in an array.
Algorithm :
Search[LA,LB,UB,K,ITEM,POS,FLAG] :
[ LA => Linear Array, LB => Lower Bound, UB => Upper Bound, ITEM => key value to be
searched, POS=> Index position of the element to be searched]
Step 1: Start
Step 2: Set LB:=0, UB:=SIZE – 1, FLAG:=0
Step 3: Display “Enter the element to be searched”
Step 4: Read ITEM
Step 5: Repeat for K:=LB to UB incremented by 1
Step 5.1: If (A[K] == ITEM), Then
Step 5.1.1: Set POS := K
Step 5.1.2: Set FLAG:=1
Step 5.1.3: Break
[End of IF – Set 5.1]
[End of For – Step 5]
Step 6: If (FLAG==1), Then
Step 6.1: Display “Item found at “ , POS
Step 7: Else
Step 7.1: Display “Element Not Found”
[ End of If – Step 6]
Step 8: Stop
INSERTION
Inserting an element: Insertion of new element to the existing list of
elements of an Array.

Algorithm :
INSERTION[LA,UB,K,ITEM,POS] :
[ LA => Linear Array, UB => Upper Bound, ITEM => element to be inserted,
POS=> Index position where new element is to be inserted]
Step 1: Start
Step 2: Display “Enter the element to be inserted and position”
Step 3: Read ITEM, POS
Step 4: Repeat for K:=UB to POS decremented by 1
Step 4.1: Set LA[K+1] := LA[K]
[End of For – Step 4]
Step 5: Set LA[POS]:= ITEM
Step 6: Set UB:=UB+1
Step 7: Stop
DELETION
Deleting an element: Deletion of an element from specified index of an Array.

Algorithm :
Search[LA,UB,K,POS] :
[ LA => Linear Array, UB => Upper Bound, POS=> Index position from
where an element is to be deleted]
Step 1: Start
Step 2: Display “Enter the index position”
Step 3: Read POS
Step 4: Repeat for K:=POS to UB incremented by 1
Step 4.1: Set LA[K] := LA[K + 1]
[End of For – Step 4]
Step 5 : Set UB:=UB – 1
Step 6: Stop
SORTING
Sorting: Arranging elements of an array in a particular order, either ascending
or descending order.
Algorithm :
Sorting[LA] :

Step 1: Start
Step 2: Let I,J,TEMP
Step 3: Repeat for J:= N – 2 to 0 by – 1
Step 3.1: Repeat for I:=0 to J by 1
Step 3.1.1: If (LA[I] > LA[I+1]), Then
Step 3.1.1.1: Set TEMP := A[I]
Step 3.1.1.2: Set LA[I] := LA [I+1]
Step 3.1.1.3: Set LA[I+1] := TEMP
[ End of If – Step 3.1.1]
[End of For – Step 3.1 ]
[End of For – Step 3]
Step 4: Stop
MERGING
Merging: Assume two arrays A and B with elements in sorted manner. Merging will
result in constructing a new array by joining elements of A and B in such way that all the
elements in the resultant array are in sorted order.
Algorithm :
Merging[A,B,C, LB1,UB1,LB2,UB2,LB3,UB3,K]
LB1 : Lower Bound of Array A, UB1: Upper Bound of Array A
LB2 : Lower Bound of Array B, UB2: Upper Bound of Array B
LB3 : Lower Bound of Array C, UB3: Upper Bound of Array C

Step 1: Start
Step 2: Let I,J,K
Step 3: Set I:=LB1,J:=LB2,K:=LB3
Step 4: Repeat while (I <=UB1) AND (J <=UB2)
Step 4.1: If ( A[I] < B[J] ), Then
Step 4.1.1: Set C[K]:= A[I], K:=K + 1, I:= I + 1
Step 4.2: Else
Step 4.2.1: Set C[K]:= B[J], K:=K + 1, J:= J + 1
[ End of If – Step 4.1]
[End of While – Step 4 ]
(Contd…)
MERGING
Step 5: Repeat while (I <=UB1) [ To copy remaining elements of array A ]
Step 5.1: Set C[K]:= A[I], K:=K + 1, I:= I + 1
[ End of While – Step 5 ]
Step 6: Repeat while ( J <=UB2) [ To copy remaining elements of array B ]
Step 6.1: Set C[K]:= B[J], K:=K + 1, J:= J + 1
[ End of While – Step 6 ]
Step 7: Stop
TWO-DIMENSIONAL ARRAY
TWO- DIMENSIONAL ARRAY
In general, a two-dimensional array is row and column arrangement of
elements. In other words, a two-dimensional array can be referred as
array of arrays.
For example : Consider A[3][4] is a two-dimensional array with 3 rows and
4 columns. In other words, we can say that there are 3 one-dimensional
arrays having 4 elements each.
Memory Representation of 2-D Array
A 2-D Array can be always represented in the form of 1 – D array in the
memory. There are two ways a 2-D array is stored in the memory. They
are:
 Row-major Order
 Column-major Order
Row-major Order
Row –major Order : In row-major order, elements of 2-D array are stored
on row-by-row basis beginning from first row i.e. majority is given to the
rows present in the matrix.
For example, above matrix A[3][4] can be represented in row-major order
in the following format:

Row-major Order Representation


Row-major Order
Finding address of an element in Row-major Order:
Assume A[M][N] is an array represented in row-major order:
Address of an element when represented in row-major order can be calculated by using
following formula:
Address of A[I][J] = B + W * ( N * I + J )
Where I = Row Index
J = Column Index
B = Base Address ( i.e. Address of 1st element)
W = Width or Size of each element
N = Column size
Ex: Assume a matrix A[3][4] is represented in memory in the form of row-major order. Find
our address of A[1][2] if base address is 1000 and size of each element is 4.

Solution:
Address of A[1][2] = 1000 + 4 ( 4 * 1 + 2 )
= 1000 + 24
= 1024
Column-major Order
Column –major Order : In column-major order, elements of 2-D array are
stored on column-by-column basis beginning from first column i.e.
majority is given to the columns present in the matrix.
For example, above matrix A[3][4] can be represented in column-major
order in the following format:

Column-major Order Representation


Column-major Order
Finding address of an element in Column-major Order:
Assume A[M][N] is an array represented in column-major order:
Address of an element when represented in column-major order can be calculated by
using following formula:
Address of A[I][J] = B + W * (I + M * J )
Where I = Row Index
J = Column Index
B = Base Address ( i.e. Address of 1st element)
W = Width or Size of each element
M = Row size
Ex: Assume a matrix A[3][4] is represented in memory in the form of column-major order.
Find our address of A[1][2] if base address is 1000 and size of each element is 4.

Solution:
Address of A[1][2] = 1000 + 4 ( 1 + 3* 2 )
= 1000 + 28
= 1028
Row & Column-major Order
Row-Major Order Address Column-Major Order Address
A[0]0] 1000 A[0][0] 1000
A[0][1] 1004 A[1][0] 1004
A[0][2] 1008 A[2][0] 1008
A[0][3] 1012 A[0][1] 1012
A[1][0] 1016 A[1][1] 1016
A[1][1] 1020 A[2][1] 1020
A[1][2] 1024 A[0][2] 1024
A[1][3] 1028 A[1][2] 1028
A[2][0] 1032 A[2][2] 1032
A[2][1] 1036 A[0][3] 1036
A[2][2] 1040 A[1][3] 1040
A[2][3] 1044 A[2][3] 1044
SPARSE MATRIX
SPARSE MATRIX
Definition: A matrix with high proportion of zero or null entries
(Usually more than half of the total elements) is called sparse matrix.

Sparse Matrix

Disadvantages:
 Representing a sparse matrix by a 2D array leads to the wastage of lots of
memory.
 The zeroes in the matrix are of no use to store zeroes with non-zero
elements.
 To avoid such wastage, we can store only non-zero elements.
 If we store only non-zero elements, it reduces the traversal time and the
storage space.
TRIPLET
Triplet : It is the alternate way of representing a sparse matrix.

Characteristics of Triplet :
• Triplet is a 2-D array having T + 1 number of rows and 3 columns ( Where
T is the total number of non-zero elements in sparse matrix)
• The first row of triplet contains number of rows, columns and non-zero
entries

Sparse Matrix Triplet


THANK YOU

You might also like