Array
Array
• DEFINITION
Example:
An array of integers to store age of students
An array of strings to store student names
TERMINOLOGY
• SIZE – no. of elements in an array
• TYPE – data type of elements
• BASE – address of memory location
where first element is stored
..
456
455
454
• Size of array : U – L + 1
• To get the size of array A[5]
• 4–0+1=5
1000
A[0]
1004
A[1]
1008
Address ( A[i] ) = M + ( i – L ) * w .
where M = base address
.
i = index j
L = lower bound .
w = word length
.
Address of A[1]=
=1000 + (1 – 0 ) * 4 100
=1004
A[100]
MEMORY ALLOCATION ….
Address mapping between logical and physical views of an array
Address ( A[i]) = M + (i – L ) * w
1. Row-Major Order
2. Column-Major Order
In Row-Major Order, elements are stored row by row.
A[0][0] A[0][1] A[0][2]
A[1][0] A[1][1] A[1][2]
A[2][0] A[2][1] A[2][2]
Stored in memory (Row-Major):
1,2,3,4,5,6,7,8,9
Address Calculation Formula (Row-Major)
Where:
LOC(A[i][j]) = address of element A[i][j],Base(A) = address of first element A[0][0]
i = row index,j = column index
N = total number of columns
W = size of each element (bytes)
Here i=2,j=1,N=3,w=4
LOC(A[2][1]) = 1000 + [(2 * 3) + 1] * 4
= 1000 + (6+1)*4
= 1000 + 28=1028
In Column-Major Order, elements are stored column by column.
Where:
LOC(A[i][j]) = address of element A[i][j],
Base(A) = address of first element A[0][0]
i = row index,j = column index
M = number of rows, W = size of each element (bytes)
Here i=2,j=1,M=3,w=4
LOC(A[2][1]) = 1000 + [(1 *3) + 2] * 4
= 1000 + (3+2)* 4
= 1000 + 20
= 1020
If bounds are given as (A[LR…... UR][LC .....UC]):
•M = (UR - LR) + 1
•N = (UC - LC) + 1
Row major :
Address 𝐴 𝑖 𝑗 = 𝐵 + 𝑊 × 𝑁 × 𝑖 − 𝐿𝑅 + 𝑗 − 𝐿𝐶
Column major:
Address 𝐴 𝑖 𝑗 = 𝐵 + 𝑊 × 𝑖 − 𝐿𝑅 + 𝑀 × 𝑗 − 𝐿𝐶
QUESTION:
B = 1025, Lr = -6, Lc = 4,
R = 10 – (-6) + 1 = 10 + 6 + 1 = 17
I = 4, J = 8, W = 4
Address of M[4][8] = B + W[R*(J – Lc) + (I – Lr)]
= 1025 + 4[17(8 – 4) + (4 – (-6))]
= 1025 + 4[17 × 4 + (4 + 6)]
= 1025 + 4[68 + 10]
= 1025 + 4 × 78
= 1025 + 312
= 1337
1)A matrix MAT[10][15] is stored in the
memory in row major wise with each
element requiring 2 bytes of storage. If the
base address at MAT[1][2] is 2215, then
the address of MAT[3][7] will be?
Ans: 2285
2) A matrix M[-2…7, 2…10] is stored in the
memory with each element requiring 2 bytes
of storage. If the base address is 2140, find the
address of M[5][6] when the matrix is stored
in row major wise.
Ans:2274
MULTI DIMENTION ARRAY:
A multidimensional array is a data structure that stores
homogeneous data elements in a tabular or grid-like format
using multiple indices. It is essentially defined as an array of
arrays, expanding the concept of a linear 1D array into two or
more dimensions.
Syntax: Defined using multiple square brackets,
e.g.,data_type array_name[size1][size2]...[sizeN];. Int
A[i][j][k]………..[n]
Total Elements: Calculated by multiplying the sizes of all its
dimensions
Total Elements = ixjxkx…………n
Example:
2D Array (Matrix): Organized into rows and columns. Example: int
matrix[3][4] creates a grid with 3 rows and 4 columns.
3D Array (Cube): Stored as a collection of 2D arrays stacked on top of
each other, using depth, rows, and columns. Example: int
cube[2][3][4]
Algorithm..
An algorithm is a step-by-step procedure to
solve a problem or achieve a desired result.
Characteristics of an Algorithm
[Link]: Takes zero or more inputs.
[Link]: Produces at least one output.
[Link]: Every step is clear and
unambiguous.
[Link]: Must terminate after a finite
number of steps.
[Link]: Each step should be simple
and executable
AYSMPTOTIC NOTATION:
Asymptotic notations are mathematical tools used to describe the
time or space complexity of an algorithm as the input size (n) grows
toward infinity.
Three Main Asymptotic Notations
The efficiency of operations on data structures (like searching,
inserting, or deleting) is categorized into three primary bounds.
OPERATIONS ON ARRAY
• Traversing
• Sorting
• Searching
• Insertion
• Deletion
• Merging
Traversing
Traversing is the process of visiting or accessing each
element of a data structure exactly once in a
systematic order
Algorithm: Traversing an Array
[Link].
[Link] the array A and its size n.
[Link] i = 0.
[Link] i < n:
[Link] A[i].
[Link] i by 1.
[Link].
#include <stdio.h>
int main()
{
int A[] = {10, 20, 30, 40, 50};
int n = 5;
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
return 0;
}
Complexity Analysis:
Time Complexity
•Best Case: O(n)
•Average Case: O(n)
•Worst Case: O(n)
Reason: The algorithm must visit all n elements of
the array, regardless of the input.
Space Complexity
•O(1) (Constant Space)
Reason: Traversing only uses a loop variable (such as
i) and does not require any additional memory
proportional to the array size.
HOME WORK
[Link].
[Link] the number of elements n.
[Link] the array elements.
[Link] for i = 0 to n - 2:
[Link] for j = 0 to n - i - 2:
[Link] A[j] > A[j + 1], swap A[j] and A[j + 1].
[Link] the sorted array.
[Link].
BUBBLE SORT: if(a[j] > a[j + 1])
#include <stdio.h> {
int main() temp = a[j];
{ a[j] = a[j + 1];
int a[10], n, i, j, temp; a[j + 1] = temp;
printf("Enter the number of elements: "); }
scanf("%d", &n); }
printf("Enter the array elements:\n"); }
for(i = 0; i < n; i++) printf("Sorted array in ascending
{ order:\n");
scanf("%d", &a[i]); for(i = 0; i < n; i++)
} {
for(i = 0; i < n - 1; i++) printf("%d ", a[i]);
{ }
for(j = 0; j < n - i - 1; j++) return 0;
{ }
• Complexity Analysis:
• Time Complexity
• Best Case: O(n) (when the array is already
sorted, with an optimized Bubble Sort)
• Average Case: O(n²)
• Worst Case: O(n²)
• Space Complexity
• O(1) (constant extra space)
SEARCHING:
This operation is used to search for an element in the given array.
This method involves traversing the dataset sequentially, checking
every element one by one(sequential /linear searching).
ALGORITHM:
[Link]
[Link] I = 0
[Link] steps 4 and 5 while I < N
[Link] A[I] is equal ITEM THEN GOTO STEP 6
[Link] I= I +1
[Link] I, ITEM
[Link]
#include <stdio.h>
void linear_search(int a[], int n, int key)
{
int i, count = 0;
for(i = 0; i < n; i++)
{
if(a[i] == key) //compares each element in array
{
printf("The element is found at %d position\n",
i+1);
count = count + 1;
}
else
printf("The element is not present in the
array\n");
}}
int main()
{
int key=18; n = 6;
int a[10] = {12, 44, 32, 18, 4, 10};
linear_search(a, n, key);
return 0;
• Complexity Analysis
• Best Case (O(1)): The target element sits at the
very first position of the collection.
• Worst Case (O(N)): The target element sits at the
last position or is missing entirely.
• Average Case (O(N)): The target element is found
somewhere in the middle of the collection.
• Space Complexity (O(1)): It requires a constant
amount of memory as no additional data
structures are created
INSERTION….
This operation is used to insert an element into the
array i.e adding a new element into an existing container
at a specific position
INSERTION …..
Algorithm : InsertArray(KEY, LOCATION)
Algorithm DELETE(KEY)
• 00304
00570
00000
02600
SPARSE MATRICES- CLASSIFICATION
Different symmetric sparse matrices
-Triangular matrices
-Upper left, upper right, lower-left, lower-right
-Diagonal matrices
-Diagonal matrices
-Tridiagonal matrices
TRIANGULAR MATRICES
DIAGONAL MATRIX - a matrix having non-zero
elements only in the diagonal running from the upper left to the
lower right or upper right to lower left
TRIDIAGONAL MATRICES -hasnonzero
elements only on the main diagonal, the first diagonal below
this, and the first diagonal above the main diagonal
EXAMPLE :
αβ- BAND MATRIX : non-zero entries are
confined to a diagonal band, comprising the main
diagonal and zero or more diagonals on either side
Array representation of Sparse matrix
2D array is used to represent a sparse matrix in which there are three rows named
as
•Row: Index of row, where non-zero element is located