0% found this document useful (0 votes)
16 views12 pages

Matrix 1

Uploaded by

mahantasuman8908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views12 pages

Matrix 1

Uploaded by

mahantasuman8908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Calculate the address of any element in the 2-D array:

The 2-dimensional array can be defined as an array of arrays. The 2-Dimensional arrays are
organized as matrices which can be represented as the collection of rows and columns as
array[M][N] where M is the number of rows and N is the number of columns.
To find the address of any element in a 2-Dimensional array there are the following two ways-
1.Row Major Order
2.Column Major Order

1. Row Major Order:


Row major ordering assigns successive elements, moving across the rows and then down
the next row, to successive memory locations. In simple language, the elements of an array
are stored in a Row-Wise fashion.
To find the address of the element using row-major order uses the following formula:

Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))


I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of the matrix(If not given assume it as
zero),
N = Number of column given in the matrix.
A matrix P[15][10] is stored with each element requiring 8 bytes of
storage. If the base address at P[0][0] is 1400, determine the address
at P[10][7] when the matrix is stored in row major wise.
B = 1400, Lr = 0, Lc = 0, C = 10, I = 10, J = 7, W = 8
Address of P[15][10] = B + W[C(I – Lr) + (J – Lc)]
= 1400 + 8[10(10 – 0) + (7 – 0)]
= 1400 + 8[10 × 10 + 7]
= 1400 + 8[100 + 7]
= 1400 + 8 × 107
= 1400 + 856
= 2256
2. Column Major Order:
If elements of an array are stored in a column-major fashion means moving across the
column and then to the next column then it’s in column-major order. To find the address of
the element using column-major order use the following formula:

Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))


I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given assume it as zero),
LC = Lower Limit of column/start column index of matrix(If not given assume it as
zero),
M = Number of rows given in the matrix.
A character array B[7][6] has a base address 1046 at 0, 0. Calculate the address at
B[2][3] if the array is stored in Column Major Wise. Each character requires two bytes
of storage.

Address of B[7][6] = B + W[r(J – Lc) + (I – Lr)]

= 1046 + 2[7(3 – 0) + (2 – 0)]

= 1046 + 2[21 + 2]

= 1046 + 46

= 1092.
What is Sparse Matrix?
In computer programming, a matrix can be defined with a 2-dimensional array. Any array with 'm'
columns and 'n' rows represent a m X n matrix. There may be a situation in which a matrix
contains more number of ZERO values than NON-ZERO values. Such matrix is known as sparse
matrix.

Sparse matrix is a matrix which contains very few non-zero elements.

When a sparse matrix is represented with a 2-dimensional array, we waste a lot of space to
represent that matrix. For example, consider a matrix of size 100 X 100 containing only 10 non-
zero elements. In this matrix, only 10 spaces are filled with non-zero values and remaining spaces
of the matrix are filled with zero. That means, totally we allocate 100 X 100 X 2 = 20000 bytes of
space to store this integer matrix. And to access these 10 non-zero elements we have to make
scanning for 10000 times. To make it simple we use the following sparse matrix representation.
Sparse Matrix Representations
A sparse matrix can be represented by using TWO representations, those are as follows...
Triplet Representation (Array Representation)
In this representation, we consider only non-zero values along with their row and column index
values. In this representation, the 0th row stores the total number of rows, total number of
columns and the total number of non-zero values in the sparse matrix.
For example, consider a matrix of size 5 X 6 containing 6 number of non-zero values. This
matrix can be represented as shown in the image...
What is Linear Search Algorithm?
Linear search is a method for searching for an element in a collection of elements. In linear
search, each element of the collection is visited one by one in a sequential fashion to find the
desired element. Linear search is also known as sequential search.

Algorithm for Linear Search Algorithm:


The algorithm for linear search can be broken down into the following steps:
• Begin at the first element of the collection of elements.
• Compare the current element with the desired element.
• If the current element is equal to the desired element, return true or index to the
current element.
• Otherwise, move to the next element in the collection.
• Repeat steps 2-4 until we have reached the end of collection.
• If the end of the collection is reached without finding the desired element, return
that the desired element is not in the array.
#include<stdio.h>
int main()
{
int a[10]={5,7,9,0,4,2,1,12,24,45};
int key,i;
printf(" element present in this array {5,7,9,0,4,2,1,12,24,45}\n");
printf("enter the element to be found\n");
scanf("%d",&key);
for(i=0;i<10;i++)
{
if(a[i]==key)
break;
}
if(i==10)
printf("element not found ");
else
printf("element found %d position\n",i+1);
}
What is Binary Search Algorithm?
Binary search is a search algorithm used to find the position of a target value within
a sorted array. It works by repeatedly dividing the search interval in half until the target value is
found or the interval is empty. The search interval is halved by comparing the target element with
the middle value of the search space.

Binary Search Algorithm:


Below is the step-by-step algorithm for Binary Search:
•Divide the search space into two halves by finding the middle index “mid”.
•Compare the middle element of the search space with the .
•If the is found at middle element, the process is terminated.
•If the is not found at middle element, choose which half will be used as the next search space.
◦If the is smaller than the middle element, then the side is used for next search.
◦If the is larger than the middle element, then the side is used for next search.
•This process is continued until the is found or the total search space is exhausted.
#include <stdio.h> int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int binarySearch(int array[], int x, int low, int high) { int n = sizeof(array) / sizeof(array[0]);
// Repeat until the pointers low and high meet each other int x = 4;
while (low <= high) { int result = binarySearch(array, x, 0, n - 1);
int mid = low + (high - low) / 2; if (result == -1)
printf("Not found");
if (array[mid] == x) else
return mid; printf("Element is found at index %d", result);
return 0;
if (array[mid] < x) }
low = mid + 1;

else
high = mid - 1;
}

return -1;
}
Types of Asymptotic Notations in Complexity Analysis of Algorithms

•Asymptotic Notations are mathematical tools used to analyze the performance of


algorithms by understanding how their efficiency changes as the input size grows.
•These notations provide a concise way to express the behavior of an algorithm’s time
or space complexity as the input size approaches infinity.
•Rather than comparing algorithms directly, asymptotic analysis focuses on
understanding the relative growth rates of algorithms’ complexities.
•Asymptotic analysis allows for the comparison of algorithms’ space and time
complexities by examining their performance characteristics as the input size varies.
•By using asymptotic notations, such as Big O, Big Omega, and Big Theta, we can
categorize algorithms based on their worst-case, best-case, or average-case time or
space complexities, providing valuable insights into their efficiency.

You might also like