0% found this document useful (0 votes)
17 views31 pages

Arrays

The document provides an overview of arrays in data structures, detailing linear arrays, their representation in memory, and operations such as insertion, deletion, and searching. It explains algorithms for linear and binary search, as well as the representation of multidimensional arrays and matrix multiplication. Additionally, it includes examples and formulas for calculating memory locations of elements in arrays.

Uploaded by

meminebd
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)
17 views31 pages

Arrays

The document provides an overview of arrays in data structures, detailing linear arrays, their representation in memory, and operations such as insertion, deletion, and searching. It explains algorithms for linear and binary search, as well as the representation of multidimensional arrays and matrix multiplication. Additionally, it includes examples and formulas for calculating memory locations of elements in arrays.

Uploaded by

meminebd
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/ 31

CSE-2101

Data Structures & Algorithms I


Rumana Yasmin
Lecturer
DCSE, FST, BUP
Chapter 4
Arrays
Array
• Data structures are classified as either linear or
nonlinear.
• A data structure is said to be linear if its elements form
a sequence or a linear list.
• There are two basic ways of representing linear
structures in memory.
• One way is to have the linear relationship between the
elements represented by means of sequential memory
locations. These linear structures are called arrays.
• The other way is to have the linear relationship
between the elements represented by means of
pointers or links. These linear structures are called
linked lists.
Linear Arrays
• A linear array is a list of finite number n of
homogeneous data elements such that :

a) The elements of the array are referenced respectively by an


index set consisting of n consecutive numbers.
b) The elements of the array are stored respectively in
successive memory locations.

• The number n of elements is called the length or


size of the array.
Linear Arrays

• Three numbers define an array : lower bound, upper


bound, size.

a. The lower bound is the smallest subscript you can use in the
array (usually 0)

b. The upper bound is the largest subscript you can use in the
array

c. The size / length of the array refers to the number of elements in


the array. It can be computed as:
upper bound - lower bound + 1

** The number k in A[k] is called a subscript and A[k] is called a subscripted variable.
Linear Arrays
Example :
A linear array DATA consisting of the name of six elements

DATA DATA[1] = 247


1 247 DATA[2] = 56
2 56 DATA[3] = 429
3 429 DATA[4] = 135
4 135
DATA[5] = 87
5 87
156 DATA[6] = 156
6
Linear Arrays

Example :
An automobile company uses an array AUTO to record the number of
‘automobile’ sold each year from 1932 through 1984.

AUTO[k] = Number of auto mobiles sold in the year K


LB = 1932
UB = 1984
Length = UB – LB + 1
= 1984 – 1932 + 1 = 53
Representation of linear array in memory
Let LA be a linear array in the memory of the computer. The memory of the
computer is a sequence of addressed locations.

The computer does not need to keep track of the


address of every element of LA, but needs to
keep track only of the first element of LA, denoted
LA by Base(LA)
1000 Called the base address of LA. Using this
1001
1002 address Base(LA), the computer calculates the
1003
address of any element of LA by the following
1004
1005 formula :
LOC(LA[k]) = Base(LA) + w(K – lower bound)
Where w is the number of words per memory cell
for the array LA
Fig : Computer memory
Representation of linear array in memory
Example :
An automobile company uses an array AUTO
to record the number of auto mobile sold each
200 year from 1932 through 1984. Suppose AUTO
201 appears in memory as pictured in fig A .
202 AUTO[1932]
203
That is Base(AUTO) = 200, and w = 4 words
204 per memory cell for AUTO. Then,
205
206
AUTO[1933] LOC(AUTO[1932]) = 200,
207 LOC(AUTO[1933]) = 204
208
AUTO[1934]
LOC(AUTO[1934]) = 208
209
210
211
212
the address of the array element for the year K = 1965 can
be obtained by using :
Fig : A
LOC(AUTO[1965]) = Base(AUTO) + w(1965 – lower bound)

= 200 + 4 (1965 - 1932) = 332


Inserting and Deleting
Inserting refers to the operation of adding another element to the Array .
Deleting refers to the operation of removing one element from the Array.
Inserting an element somewhere in the middle of the array require that
each subsequent element be moved downward to new locations to
accommodate the new element and keep the order of the other elements.
Deleting an element somewhere in the middle of the array require that
each subsequent element be moved one location upward in order to “fill
up” the array.
Fig shows Milon Inserted at index 3, and Sumona deleted.

STUDENT STUDENT STUDENT


Dalia Rahaman
1 Dalia Rahaman 1 Dalia Rahaman 1
Sumona Sumona Milon
2 2 2
Mubtasim Fuad Milon Mubtasim Fuad
3 3 3
Anamul Haque Mubtasim Fuad Anamul Haque
4 4 4
Anamul Haque
5 5 5
6 6 6
Insertion
INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
Here LA is linear array with N elements and K is a positive integer such
that K<=N. This algorithm inserts an element ITEM into the Kth position in
LA.
ALGORITHM :

Step 1. [Initialize counter] Set J:=N Repeat Steps


Step 2. 3 and 4 while J >= K
Step 3. [Move Jth element downward] Set LA[J+1] := LA[J]
Step 4. [Decrease counter] Set J := J - 1
[End of step 2 loop]
Step 5 [Insert element] Set LA [K] := ITEM
Step 6. [Reset N] Set N := N+1
Step 7. Exit
Deletion
DELETING AN ELEMENT FROM A LINEAR ARRAY
Delete (LA, N, K, ITEM)

ALGORITHM

Step 1. Set ITEM: = LA [K]


Step 2. Repeat for J=K to N-1
[Move J+1 st element upward] Set LA [J]: =LA [J+1]
[End of loop]
Step 3 [Reset the number N of elements in LA] Set N:=N-1
Step 4. Exit.
Searching : Linear search
• Searching refers to the operation of finding the location LOC of
ITEM in DATA, or printing some message that ITEM does not appear
there.

• Suppose, DATA is a linear array with n elements. The most intuitive


way to search for a given ITEM in DATA is to compare ITEM with each
element of DATA one by one.

• That is first we test whether DATA[1 ]=ITEM, and then we test whether
DATA[2 ]=ITEM, and so on.

• This method, which traverses DATA sequentially to locate ITEM, is


called linear search or sequential search.
Linear Search (search = 33)
Searching : Linear search
Algorithm : A linear array DATA with N elements and a specific ITEM of
information are given. This algorithm finds the location LOC of ITEM in
the array DATA or sets LOC = 0 if the search is unsuccessful.

1. Read: ITEM.
2. Set LOC:=1.
3. Repeat while DATA[LOC]!= ITEM and LOC <=N:
Set LOC := LOC +1.
[ End of loop]
4. If LOC = N+1, then :
Write : ITEM is not in the array DATA.
Else :
Write : LOC is the location of ITEM.
5. Exit.
Linear Search Applications
• For searching operations in smaller arrays (<100
items).
Searching : Binary search
• Binary search is the most popular Search
algorithm. It is efficient and one of the most used
techniques that is used to solve problems.

• Binary search works only on a sorted set of


elements. To use binary search on a collection,
the collection must first be sorted.
Binary Search example
(Search for ITEM = 7)
Binary Search Algorithm
BINARY(DATA, LB, UB, ITEM, LOC)
1. Set BEG=LB; END=UB; and MID=INT((BEG+END)/2).
2. Repeat step 3 and 4 while BEG < END and DATA[MID] ≠ ITEM
3. If ITEM < DATA[MID] then
Set END= MID - 1
Else:
Set BEG= MID+1
[end of if structure]
4. Set MID= INT((BEG+END)/2)
[End of step 2 loop]
5. If ITEM = DATA[MID] then
Set LOC= MID
Else:
Set LOC= NULL
[end of if structure]
6. Exit.
Time Complexity: O(log N)
Binary Search Implementation
Binary Search Implementation
Observation:

• In binary search, using mid = lo + (hi - lo) / 2 instead


of mid = (lo + hi) / 2 helps prevent integer overflow,
especially when dealing with large values of lo and
hi.

• If lo and hi are close to the maximum value that an


integer can hold, adding them together might exceed
the maximum value, leading to an overflow.
Binary Search Applications
• In libraries of Java, .Net, C++ STL
• While debugging, the binary search is used to
pinpoint the place where the error happens.
Multidimensional arrays
Two – dimensional Arrays
A Two – dimensionalArray 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.
The element of A with first subscript J and second subscript K will be denoted
by A[J, K]

Columns

1 2 3
1 A[1, 1] A[1, 2] A[1, 3]
Rows 2
A[2, 1] A[2, 2] A[2, 3]
3
A[3, 1] A[3, 2] A[3, 3]

Fig: Two dimensional 3 x 3 array A


Matrix Multiplication

Algorithm 4.7: MATMUL(A, B, C, M, P, N)


Let A be an MXP matrix array, and let B be a PXN matrix array. This
algorithm stores the product of A and B in an MXN matrix array.

1. Repeat steps 2 to 4 for I =1 to M:


2. Repeat steps 3 to 4 for J =1 to N:
3. Set C[I, J]:=0
4.Repeat for K =1 to P:
C[I, J]:= C[I, J]+A[I, K]*B[K, J]
5. Exit
Storage Representation
• Row major representation

• Column major representation


Storage Representation
For any two-dimensional M x N array, the location
can be calculated using the formula below:

• Row major representation


Loc(A[J, K]) = Base(A) + w[N(J-1) + (K-1)]

• Column major representation


Loc(A[J, K]) = Base(A) + w[(J-1) + M(K-1)]
Example-1
• Consider a 25 x 4 matrix array SCORE, where
Base(SCORE) = 200 and w = 4 words per memory cell
• Furthermore, suppose the programming
language stores two dimensional arrays using
row-major order.
• Find the address of SCORE[12, 3].

LOC(SCORE[12,3]) = 200 + 4 [4(12-1) + (3-1)] = 384


Example-2
• Suppose a 3D array MAZE is declared using
MAZE(2:8, -4:1, 6:10)
• Suppose the programming language stores MAZE
in memory in row-major order and Base(MAZE) =
200, and there are w = 4 words per memory cell
• Find the address of the element: MAZE[5, -1, 8]

• Formula:
Base(C) + w[(….((E1L2 + E2)L3 + E3)L4 +…. + EN-1)LN + EN]
Solution
• The lengths of the three dimensions of MAZE are:
L1 = 8 – 2 + 1 = 7
L2 = 1 –(-4) + 1 = 6
L3 = 10 – 6 + 1 = 5
• Total number of elements in MAZE = 7 x 6 x 5 = 210
• To find the address of the element MAZE[5, -1, 8] we
need to calculate the effective index (E)
E1 = 5 – 2 = 3
E2 = -1 – (-4) = 3
E3 = 8 – 6 = 2
• (E1L2 + E2)L3 + E3 = 107
• Therefore, LOC(MAZE[5, -1, 8]) = 200 + 4(107) = 628
Reference
• DSA Linear Search
• DSA Binary Search
• https://www.programiz.com/dsa/linear-search
• https://www.programiz.com/dsa/binary-search
• https://www.cs.usfca.edu/~galles/visualization/Search.html

You might also like