print msc
DESCRIPTION
algorithm lab program for msc(cs)TRANSCRIPT
![Page 1: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/1.jpg)
Divide and Conquer
Merge Sort
Quick Sort
Maximum and Minimum
![Page 2: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/2.jpg)
MERGE SORT#include<stdio.h>#include<conio.h>void mergesort(int a[],int i,int j);void merge(int a[],int i1,int j1,int i2,int j2);void main(){int a[30],n,i;clrscr();printf("enter the 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("\n sorted array is:");for(i=0;i<n;i++)printf("%d",a[i]);getch();}
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);}}
![Page 3: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/3.jpg)
void merge(int a[],int i1,int j1,int i2,int j2){int temp[50];int i,j,k;i=i1;j=i2;k=0;while(i<=j1&&j<=j2){if(a[i]<a[j])temp[k++]=a[i++];elsetemp[k++]=a[j++];}while(i<=j1)temp[k++]=a[j++];while(j<=j2)temp[k++]=a[j++];for(i=i1,j=0;i<=j2;i++,j++)a[i]=temp[j];}
![Page 4: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/4.jpg)
Output:Enter the no. Of elements: 5
Enter array elements:1005902030
Sorted array is:5203090100
![Page 5: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/5.jpg)
Quick Sort#include<stdio.h>
#include<conio.h>
void quicksort(int array[20],int first,int last);
void main()
{
int array[30];
int i,size;
clrscr();
printf("enter the total number of elements:");
scanf("%d",&size);
printf("enter the total %d elements:\n",size);
for(i=0;i<size;i++)
scanf("%d",&array[i]);
quicksort(array,0,size-1);
printf("quicksorted elements are as:\n");
for(i=0;i<size;i++)
printf("%d\t",array[i]);
getch();
}
void quicksort(int array[20],int first,int last)
{
int i,j,pivot,temp;
if(first<last)
{
![Page 6: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/6.jpg)
pivot=first;
i=first;
j=last;
while(i<j)
{
while(array[i]<=array[pivot]&&i<last)
i++;
while(array[j]>array[pivot])
j--;
if(i<j)
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
temp=array[pivot];
array[pivot]=array[j];
array[j]=temp;
quicksort(array,first,j-1);
quicksort(array,j+1,last);
}
}
![Page 7: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/7.jpg)
![Page 8: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/8.jpg)
MAXIMUM AND MINIMUM#include<stdio.h>#include<conio.h>void main(){int a[10],i,n,max,min;clrscr();printf("enter the value of n:");scanf("%d",&n);printf("\n");for(i=0;i<n;i++){printf("enter the %delements",i+1);scanf("%d",&a[i]);}max=a[0];min=a[0];for(i=0;i<n;i++){if(a[i]>max)max=a[i];if(a[i]<min)min=a[i];}printf("\n the maximum number is %d\n",max);printf("\n the minimum number is %d\n",min);getch();}
![Page 9: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/9.jpg)
![Page 10: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/10.jpg)
Greedy Method
Knap sack Problem
Tree Vertex Splitting
Job Sequencing
![Page 11: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/11.jpg)
KNAPSACK PROBLEM#include<stdio.h>#include<conio.h>void knapsack(int n,float weight[],float profit[],float capacity){float x[20],tp=0;int i,j,u;u=capacity;for(i=0;i<n;i++)x[i]=0.0;for(i=0;i<n;i++){if(weight[i]>u)break;else{x[i]=1.0;tp=tp+profit[i];u=u-weight[i];}}if(i<n)x[i]=u/weight[i];tp=tp+(x[i]*profit[i]);printf("\nthe result vector is:");for(i=0;i<n;i++)printf("%f\t",x[i]);printf("\nmaximum profit is:%f",tp);}int main(){float weight[20],profit[20],capacity;int num,i,j;float ratio[20],temp;clrscr();
![Page 12: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/12.jpg)
printf("\nenter the no of objects:");scanf("%d",&num);printf("\nenter the wts and profit of each object:");for(i=0;i<num;i++){scanf("%f%f",&weight[i],&profit[i]);}printf("\nenter the capacity of knapsack:");scanf("%f",&capacity);for(i=0;i<num;i++){ratio[i]=profit[i]/weight[i];}for(i=0;i<num;i++){for(j=i+1;j<num;j++){if(ratio[i]<ratio[j]){temp=ratio[j];ratio[j]=ratio[i];ratio[i]=temp;temp=weight[j];weight[j]=temp;temp=profit[j];profit[j]=profit[i];profit[i]=temp;}}}knapsack(num,weight,profit,capacity);return(0);}
![Page 13: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/13.jpg)
![Page 14: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/14.jpg)
TREE VERTEX SPLITTING#include <stdio.h>#include <stdlib.h> int min(int x, int y) { return ( (x < y)? x: y);)} struct node{ int data; int vc; struct node *left, *right;}; int vCover(struct node *root){ if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) return 0; if (root->vc != 0) return root->vc; int size_incl = 1 + vCover(root->left) + vCover(root->right); int size_excl = 0; if (root->left) size_excl += 1 + vCover(root->left->left) + vCover(root->left->right); if (root->right) size_excl += 1 + vCover(root->right->left) + vCover(root->right->right); root->vc = min(size_incl, size_excl); return root->vc;} struct node* newNode( int data ){ struct node* temp = (struct node *) malloc( sizeof(struct node) ); temp->data = data;
![Page 15: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/15.jpg)
temp->left = temp->right = NULL; temp->vc = 0; return temp;} int main(){struct node *root = newNode(20);root->left = newNode(8);root->left->left = newNode(4);root->left->right = newNode(12);root->left->right->left = newNode(10);root->left->right->right = newNode(14);root->right = newNode(22);root->right->right = newNode(25); printf ("Size of the smallest vertex cover is %d ", vCover(root)); return 0;}
![Page 16: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/16.jpg)
![Page 17: Print Msc](https://reader036.vdocument.in/reader036/viewer/2022083005/563db7b9550346aa9a8d58ad/html5/thumbnails/17.jpg)