Unit 2 Array
Unit 2 Array
What is array
Linear structure of the data elements in the memory.
Arrays are among the oldest and most important data structures, and are used by almost every
program.
Arrays are useful mostly because the element indices can be computed at run time. Among other
things, this feature allows a single iterative statement to process arbitrarily many elements of an
array.
• Deletion
Single/One Dimensional Array
A one-dimensional array (or single dimension array) is a type of linear
array.
• LOC(LA[K]=Base(LA)+w(K-lower bound).
Q1. Data[] is an array that is declared as intData[20]; and contains the following
values:
Data[] = {12, 23, 34, 45, 56, 67, 78, 89, 90, 100};
(a) Calculate the length of the array.
(b) Find the upper_bound and lower_bound.
(c) Show the memory representation of the array.
Traversing an Array
In Step 1, we initialize the index to the lower bound of the array. In Step 2, a while loop is
executed. Step 3 processes the individual array element as specified by the array name and index
value. Step 4 increments the index value so that the next array element could be processed. The
while loop in Step 2 is executed until all the elements in the array are processed, i.e., until I is
less than or equal to the upper bound of the array.
Both insertion and deletion can take place at the following positions:
a. At start
b. In between
c. At end
If an element has to be inserted at the end of an existing array, then the task of insertion is quite
simple. We just have to add 1 to the upper_bound and assign the value.
The algorithm INSERT will be declared as INSERT (A, N, POS, VAL). The arguments are
(a) A, the array in which the element has to be inserted
(b) N, the number of elements in the array
(c) POS, the position at which the element has to be inserted
(d) VAL, the value that has to be inserted
(a) Since the elements of the array are stored in ascending order, we will compare the value that has to
be deleted with the value of every element in the array. As soon as VAL = Data[I], where I is the index or
subscript of the array, we will get the position from which the element has to be deleted. For example, if
we see this array, here VAL = 56. Data[0] = 12 which is not equal to 56. We will continue to compare and
finally get the value of POS = 4.
Advantages and Disadvantages of linear
search
• Advantages
Easy to understand and simple
Does not require a sorted array
Handy and practical approach when the list to be searched has only a
few elements.
Disadvantages:
• This search algorithm works on the principle of divide and conquer. For this algorithm to work
properly, the data collection should be in the sorted form.
• Binary search can be used only with list of element which are already arranged in a order. The
binary search can not be used for list of element which are in random order.
• This search process starts comparing of the search element with the middle element in the list. If
both are matched, then the result is "element found".
• Otherwise, we check whether the search element is smaller or larger
than the middle element in the list. If the search element is smaller,
then we repeat the same process for left sublist of the middle
element. If the search element is larger, then we repeat the same
process for right sublist of the middle element. We repeat this
process until we find the search element in the list or until we left
with a sublist of only one element. And if that element also doesn't
match with the search element, then the result is "Element not found
in the list".
• Step 1: [INITIALIZE] SET BEG = lower_bound , END = upper_bound, MID= INT
((BEG+END)/2).
• Step 2: Repeat Steps 3 and 4 while BEG <= END and DATA [MID] not equal VAL
• Step 3: SET MID = (BEG + END)/2
• Step 4: IF A[MID] = VAL PRINT VAL Go to Step 7
ELSE IF A[MID] > VAL
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
Step 5: set MID= INT((BEG + END)/2)
[END OF LOOP]
Step 6: IF DATA[MID]= VAL
SET LOC=MID
ELSE
SET LOC=NULL
• [END OF IF]
• Step 7: EXIT
Given a list of numbers a[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21}. Search
for value 19 using
1. Linear search
2. Binary search technique.
• Complexity of Binary Search Algorithm
• best case is when the key is equal to the first element compared from
the list, in which case only one comparison is needed.
• The worst case occurs when the key is present in the list but at such a
position/ index that it requires more comparisons to find it.
• Another possible worst case is when the value is not present in the
list.
Two Dimensional Array
• An array of arrays is known as 2D array.
• The two dimensional (2D) array is also known as matrix. A matrix can be
represented as a table of rows and columns.
• Similar to the 1D array, we must specify the data type, the name, and
the size of the array. But the size of the array is described as the
number of rows and number of columns.
E.G:
Int a[rows][cols];
Access data in a 2D array:
Example
Given array int A(2 : 5, - 3 : 1)
1.The lengeh of row one-dimensional array (5 - 2 +1 = 4)
2.The length of column one-dimensional array (1 - (-3) +1=5)
3.Length of given array = 4 x 5 = 20
So, there are 20 elements on the given array.
Address calculation of 2D Array
(Column major order)
LOC(A[I,J])=Base(A)+w(M(J-1)+(I-1)]
NOTE:- where w is the number of bytes required to store one element, N is the number of columns,
M is the number of rows, and I and J are the subscripts of the array element.
Q1. Consider the 25 x 4 matrix array SCORE. Suppose, base (SCORE) = 200 and there are w
= 4 words Per memory cell. Furthermore, suppose the programming_ language stores two-
dimensional array using row-order. The address of SCORE [12, 3] follows,
Q2. Consider a 20 \ 5 two-dimensional array marks which has its base address = 1000
and the size of an element = 2. Now compute the address of the element, marks[18][ 4] assuming
that the elements are stored in row major order.
Q3. Calculate the address of x[4,3] in a 2-d array x(1..5,1..4) stored in a row-major order in the main memory.
Assume the base address to be 1000 and that each element requires 4 words of storage.
Q4. arr[4:7;-1:3], 2 bytes of storage of each element, Find Address of Arr[6][2], base address is 100
SOL 1.
LOC (SCORE [12, 3]) = 200 + 4 [4(12 -1) + (3-1)] = 200 + 4 [46] = 384
Sol 2.
Address(A[I][J]) = Base_Address + w{N (I – 1) + (J – 1)}
Address(marks[18][4]) = 1000 + 2 {5(18 – 1) + (4 – 1)}
= 1000 + 2 {5(17) + 3}
= 1000 + 2 (88)
= 1000 + 176 = 1176
OPERATIONS ON TWO-DIMENSIONAL ARRAYS
Two-dimensional arrays can be used to implement the mathematical concept of matrices. In
mathematics, a matrix is a grid of numbers, arranged in rows and columns. Thus, using two dimensional
arrays, we can perform the following operations on an m×n matrix:
2. Sum Two matrices that are compatible with each other can be added together, storing the result
in the third matrix. Two matrices are said to be compatible when they have the same number of
rows and columns. The elements of two matrices can be added by writing:
Ci,j = Ai,j + Bi,j
3. Difference Two matrices that are compatible with each other can be subtracted, storing the result
in the third matrix. Two matrices are said to be compatible when they have the same number of
rows and columns. The elements of two matrices can be subtracted by writing:
Ci,j = Ai,j – Bi,j
4. Product Two matrices can be multiplied with each other if the number of columns in the first
matrix is equal to the number of rows in the second matrix. Therefore, m \ n matrix A can be
multiplied with a p \ q matrix B if n=p. The dimension of the product matrix is m \ q. The elements
of two matrices can be multiplied by writing:
Ci,j = S Ai,kBk,j for k = 1 to n