0% found this document useful (0 votes)
35 views10 pages

Prog

The document contains code for three algorithms: 1) Merge sort and quick sort sorting algorithms, including functions to merge arrays, recursively sort arrays, and print sorted arrays. 2) Addition and multiplication of polynomials by representing polynomials as structures containing coefficient and exponent terms, and defining functions for initializing polynomials, appending terms, adding, and multiplying polynomials. 3) Lowest common subsequence algorithm that uses dynamic programming to find the longest common subsequence between two strings by building a 2D matrix and returning the maximum value.

Uploaded by

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

Prog

The document contains code for three algorithms: 1) Merge sort and quick sort sorting algorithms, including functions to merge arrays, recursively sort arrays, and print sorted arrays. 2) Addition and multiplication of polynomials by representing polynomials as structures containing coefficient and exponent terms, and defining functions for initializing polynomials, appending terms, adding, and multiplying polynomials. 3) Lowest common subsequence algorithm that uses dynamic programming to find the longest common subsequence between two strings by building a 2D matrix and returning the maximum value.

Uploaded by

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

1(a).

Merge Sort
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
using namespace std;
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for(i = 0; i < n1; i++)
L[i] = arr[l + i];
for(j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], int size)

{
int i;
for (i=0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main()
{
int arr_size,arr[1005],i;
cin>>arr_size;
for(i=0;i<arr_size;i++)
cin>>arr[i];
printf("Given array is \n");
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}

1(b).Quick Sort
#include<stdio.h>
#include<iostream>
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int l, int h)
{
int x = arr[h];
int i = (l - 1);
for (int j = l; j <= h- 1; j++)
{
if (arr[j] <= x)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[h]);
return (i + 1);
}
void quickSort(int arr[], int l, int h)
{
if (l < h)
{
int p = partition(arr, l, h);
quickSort(arr, l, p - 1);
quickSort(arr, p + 1, h);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int n,arr[10005],i;
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

2.Addition and multiplication of two polynomials


#include <stdio.h>
#include<iostream>
using namespace std;
#define MAX 10
struct term
{
int coeff ;
int exp ;
};
struct poly
{
struct term t [10] ;
int noofterms ;
};
void initpoly ( struct poly *) ;
void polyappend ( struct poly *, int, int ) ;
struct poly polyadd ( struct poly, struct poly ) ;
struct poly polymul ( struct poly, struct poly ) ;
void display ( struct poly ) ;
int main()
{
int n,a,b;
struct poly p1, p2, p3,p4 ;
initpoly ( &p1 ) ;
initpoly ( &p2 ) ;
initpoly ( &p3 ) ;
cout<<"Enter number of terms of first polynomial:";
cin>>n;
cout<<"Enter coefficients and powers of the first polynomial:";
while(n--)
{
cin>>a>>b;
polyappend ( &p1, a, b ) ;
}
cout<<"Enter number of terms of first polynomial:";
cin>>n;
cout<<"Enter coefficients and powers of the first polynomial:";
while(n--)
{
cin>>a>>b;
polyappend ( &p2, a, b ) ;
}
p3 = polymul ( p1, p2 ) ;
p4=polyadd(p1,p2);
printf ( "\nFirst polynomial:\n" ) ;
display ( p1 ) ;
printf ( "\n\nSecond polynomial:\n" ) ;
display ( p2 ) ;
printf("\n\nAddition of two polynomials:\n");
display(p4);
printf ( "\n\nMultiplication of two polynomials:\n" ) ;
display ( p3 ) ;
cout<<endl;

return 0;
}
void initpoly ( struct poly *p )
{
int i ;
p -> noofterms = 0 ;
for ( i = 0 ; i < MAX ; i++ )
{
p -> t[i].coeff = 0 ;
p -> t[i].exp = 0 ;
}
}
void polyappend ( struct poly *p, int c, int e )
{
p -> t[p -> noofterms].coeff = c ;
p -> t[p -> noofterms].exp = e ;
( p -> noofterms ) ++ ;
}
void display ( struct poly p )
{
int flag = 0, i ;
for ( i = 0 ; i < p.noofterms ; i++ )
{
if ( p.t[i].exp != 0 )
printf ( "%d x^%d + ", p.t[i].coeff, p.t[i].exp ) ;
else
{
printf ( "%d", p.t[i].coeff ) ;
flag = 1 ;
}
}
if ( !flag )
printf ( "\b\b " ) ;
}
struct poly polyadd ( struct poly p1, struct poly p2 )
{
int i, j, c ;
struct poly p3 ;
initpoly ( &p3 ) ;
if ( p1.noofterms > p2.noofterms )
c = p1.noofterms ;
else
c = p2.noofterms ;
for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ )
{
if ( p1.t[i].coeff == 0 && p2.t[j].coeff == 0 )
break ;
if ( p1.t[i].exp >= p2.t[j].exp )
{
if ( p1.t[i].exp == p2.t[j].exp )
{
p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ;
p3.t[p3.noofterms].exp = p1.t[i].exp ;

i++ ;
j++ ;
}
else
{
p3.t[p3.noofterms].coeff = p1.t[i].coeff ;
p3.t[p3.noofterms].exp = p1.t[i].exp ;
i++ ;
}
}
else
{
p3.t[p3.noofterms].coeff = p2.t[j].coeff ;
p3.t[p3.noofterms].exp = p2.t[j].exp ;
j++ ;
}
}
return p3 ;
}
struct poly polymul ( struct poly p1, struct poly p2 )
{
int coeff, exp ;
struct poly temp, p3 ;
initpoly ( &temp ) ;
initpoly ( &p3 ) ;
if ( p1.noofterms != 0 && p2.noofterms != 0 )
{
int i ;
for ( i = 0 ; i < p1.noofterms ; i++ )
{
int j ;
struct poly p ;
initpoly ( &p ) ;
for ( j = 0 ; j < p2.noofterms ; j++ )
{
coeff = p1.t[i].coeff * p2.t[j].coeff ;
exp = p1.t[i].exp + p2.t[j].exp ;
polyappend ( &p, coeff, exp ) ;
}
if ( i != 0 )
{
p3 = polyadd ( temp, p ) ;
temp = p3 ;
}
else
temp = p ;
}
}
return p3 ;
}

3.Lowest Common Subsequence


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
int max(int a, int b);
int lcs( char *X, char *Y, int m, int n )
{
int L[m+1][n+1];
int i, j;
for (i=0; i<=m; i++)
{
for (j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[m][n];
}
int max(int a, int b)
{
return (a > b)? a : b;
}
int main()
{
char X[105],Y[105];
cin>>X;
cin>>Y;
int m = strlen(X);
int n = strlen(Y);
printf("Length of LCS is %d\n", lcs( X, Y, m, n ) );
return 0;
}

ADVANCED
PROGRAMMING
FILE

MADE BY:
SRISHTI
774/IT/12

You might also like