0% found this document useful (0 votes)
30 views

Brief - Report - CSE 01706543

The document discusses matrix multiplication and its implementation in C. Matrix multiplication involves multiplying two matrices where the number of columns of the first matrix must equal the rows of the second. The code provided demonstrates multiplying two matrices and outputting the result.

Uploaded by

Match Maker
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)
30 views

Brief - Report - CSE 01706543

The document discusses matrix multiplication and its implementation in C. Matrix multiplication involves multiplying two matrices where the number of columns of the first matrix must equal the rows of the second. The code provided demonstrates multiplying two matrices and outputting the result.

Uploaded by

Match Maker
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/ 15

1. Implementation Of Insertion Sort Using C Program.

Insertion Sort is a simple sorting algorithm that builds the


final sorted array one item at a time. This is an in-place comparison-
based sorting algorithm. Here, a sub-list is maintained which is always
sorted. For example, the lower part of an array is maintained to be
sorted. An element which is to be 'insert'ed in this sorted sub-list, has to
find its appropriate place and then it has to be inserted there.
Hence the name, insertion sort.
Use Of Insertion Sort
Insertion sort is used when number of elements is small. It can also be
useful when input array is almost sorted, only few elements are
misplaced in complete big array.
Advantage Of Insertion Sort:

a) The main advantage of the insertion sort is its simplicity.


b) Simple to code.
c) Very good performance with small lists.
d) Very good when the list is almost sorted. In this case very few
compares need to be done.
e) It keeps the relative positions of the elements intact
f) Very memory efficient - it only needs one extra storage location to
make room for the moving items.
g) Good with sequential data that is being read in one at a time.

Disadvantage Of Insertion Sort


Poor performance with large lists.The disadvantage of the insertion sort
is that it does not perform as well as other, better sorting algorithms.
With n-squared steps required for every n element to be sorted, the
insertion sort does not deal well with a huge list. Therefore, the
insertion sort is particularly useful only when sorting a list of few items.
Source Code:
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
int n, i, j, key, array[100];
printf("\n\tEnter number of Elements = ");
scanf("%d", &n);
printf("\tEnter %d Integers = \n", n);
for (j = 1; j <= n; j++)
{
printf("\n\tEnter Integer value = ");
scanf("\t%d", &array[j]);
}
for (j = 2; j <=n; j++){
key = array[j];
i = j - 1;
while (i > 0 && array[i] > key)
{
array[i + 1] = array[i];
i = i - 1;
}
array[i + 1] = key;
}
printf("\n\tSorted list in ascending order =\n");
for (i = 1; i <= n; i++)
printf("\t%d\n", array[i]);
getch();
}

OUTPUT:

Fig : Insertion Sort Output


2. Implementation Of Merge Sort Using C Program.

Merge sort is a divide-and-conquer algorithm based on the idea of


breaking down a list into several sub-lists until each sub list consists of a
single element and merging those sub list in a manner that results into a
sorted list.
Use Of Merge Sort
E-commerce website/platform
E-commerce websitemaintains an array of it’s all users with their
activities on the website and when the have some latest products in their
latest products list which also another array the devide those product in
array according to user’s array of choice
Another mega example of using merge sorting is two of india’s biggest
e-commerce website policybazar.com and trivago.com who use merge
sorting for creating best price and stuff for it’s users.
Advantage of merge sort
It is quicker for larger lists because unlike insertion and bubble sort it
doesnt go through the whole list seveal times. It has a consistent running
time, carries out different bits with similar times in a stage.
Disadvantage of merge sort
Slower comparative to the other sort algorithms for smaller tasks.
goes through the whole process even i he list is sorted (just like insertion
and bubble sort?). Uses more memory space to store the sub elements of
the initial split list.
Evaluation
Source Code:
#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j) {
int mid;
if(i<j) {
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j); } }
void merge(int a[],int i1,int j1,int i2,int j2) {
int temp[50] ,i,j,k;
i=i1;j=i2;k=0;
while(i<=j1 && j<=j2) {
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++]; }
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j]; }
OUTPUT:

Fig : Marge Sort Output


3. Implementation Of Longest Common Subsequence.
The longest common subsequence problem is finding the longest
sequence which exists in both the given strings.
Use Of LCS
In Google Maps to find the shortest path between source and the series
of destination (one by one) out of the various available paths.
In networking to transfer data from a sender to various receivers in a
sequential manner. Largest common subsequence - to identify similar
videos used by youtube. Find minimum number of deletions required to
convert a string into palindrome. Longest Increasing Subsequence
using LCS. Document Distance Algorithms- to identify the extent of
similarity between two text documents used by Search engines like
Google, Wikipedia, Quora and other websites.
Advantage of LCS
Advantages of this method include the fact that iteration is usually faster
than recursion, we don't need to initialize the matrix to all -1's, and we
save three if statements per iteration since we don't need to test whether
L[i,j], L[i+1,j], and L[i,j+1] have already been computed (we know in
advance that the answers will be no, yes, and yes).
Disadvantage of LCS
One disadvantage over memorizing is that this fills in the entire array
even when it might be possible to solve the problem by looking at only a
fraction of the array's cells. The dynamic programming methods we've
described, compared to the original recursion, is that they use a lot of
space: O(mn) for the array.
Source Code:
#include<stdio.h>

#include<conio.h>

#include<string.h>

int i,j,m,n,c[20][20];

char x[20],y[20],b[20][20];

void print(int i,int j) {

if(i==0 || j==0)

return;

if(b[i][j]=='c') {

print(i-1,j-1);

printf("%c",x[i-1]); }

else if(b[i][j]=='u')

print(i-1,j);

else

print(i,j-1); }

void lcs() {

m=strlen(x);

n=strlen(y);

for(i=0;i<=m;i++)

c[i][0]=0;

for(i=0;i<=n;i++)

c[0][i]=0;
for(i=1;i<=m;i++)

for(j=1;j<=n;j++) {

if(x[i-1]==y[j-1]) {

c[i][j]=c[i-1][j-1]+1;

b[i][j]='c'; }

else if(c[i-1][j]>=c[i][j-1]) {

c[i][j]=c[i-1][j];

b[i][j]='u'; }

else {

c[i][j]=c[i][j-1];

b[i][j]='l'; }

int main() {

clrscr();

printf("\n\tEnter 1st string = ");

scanf("%s",&x);

printf("\n\t1st string is = %s",x);

printf("\n\tEnter 2nd string = ");

scanf("%s",&y);

printf("\n\t2nd string is = %s",y);

m=strlen(x);
printf("\n\t1st string length is = %d",m);

n=strlen(y);

printf("\n\t2nd string length is = %d",n);

printf("\n\n\tThe longest common subsequence is = ");

lcs(); print(m,n);

getch();

OUTPUT

Fig : LCS Output


4. Implementation Of Matrix Multiplication.

In mathematics, matrix multiplication is a binary operation that


produces a matrix from two matrices. For matrix multiplication, the
number of columns in the first matrix must be equal to the number of
rows in the second matrix. The result matrix, known as the matrix
product, has the number of rows of the first and the number of columns
of the second matrix.
Use Of Matrix
If you think, there are many game application where you can use Multi-
dimensional array. If we talk about game then chess is first game where
rows and columns are used. Second is tic-toe game and other board
games are there too. Apart from games these can be used in matrix
calculations and lots of other areas depending on requirement.
Advantage of 2d Array
 Matrices are efficient data structures to store multiple vectors.
 Arrays represent multiple data items of the same type using a
single name.
 In arrays, the elements can be accessed randomly by using the
index number.
 Arrays allocate memory in contiguous memory locations for all its
elements. Hence there is no chance of extra memory being
allocated in case of arrays. This avoids memory overflow or
shortage of memory in arrays.
 The modern graphics pipeline is shadier-based and requires the
client application to send data quickly.
 It can be used to implement other data structures like linked lists,
stacks, queues, trees, graphs etc.
 2D arrays are used to represent matrices.
Disadvantage of 2d Array

 A disadvantage of the matrix structure is the increased complexity.


The number of elements to be stored in an array should be known
in advance.
 An array is a static structure (which means the array is of fixed
size). Once declared the size of the array cannot be modified. The
memory which is allocated to it cannot be increased or decreased.
 Insertion and deletion are quite difficult in an array as the elements
are stored in consecutive memory locations and the shifting
operation is costly.
 Allocating more memory than the requirement leads to wastage of
memory space and less allocation of memory also leads to a
problem.

Source Code:

#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("\n\tEnter number of rows and columns of first matrix\n\t");
scanf("%d%d", &m, &n);
printf("\n\tEnter elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
{
printf("\n\t");
scanf("%d", &first[c][d]);
}
printf("\n\tEnter number of rows and columns of second matrix\n\t");
scanf("%d%d", &p, &q);
if (n != p)
printf("\n\tThe multiplication isn't possible.\n");
else {
printf("\tEnter elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++) {
printf("\n\t");
scanf("%d", &second[c][d]); }
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d]; }
multiply[c][d] = sum;
sum = 0; } }
printf("\n\tProduct of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("\t%d", multiply[c][d]);
printf("\n"); } }
getch(); }
Output:

Fig : Matrix Multiplication Output

You might also like