thesupplier4u.files.wordpress.com · web viewimplementation and time analysis of sorting...

57
XXXXXXXXXXXX PRACTICAL – 1 AIM : Implementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort. Code : #include<stdio.h> #include<stdlib.h> #include<time.h> #define size 900 int main() { int i,j,a[900],t,p,min; clock_t start,end; double cpu; /* BUBBLE SORT */ for(i=0;i<size;i++) a[i]=rand()%100; start=clock(); for(i=0;i<size;i++) { for(j=0;j<size;j++) { if(a[j+1]<a[j]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } end=clock(); cpu=((double)(end-start)/CLOCKS_PER_SEC); CSE-1(2150703) 1

Upload: others

Post on 02-Dec-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 1

AIM : Implementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort.

Code :

#include<stdio.h>#include<stdlib.h>#include<time.h>#define size 900

int main(){int i,j,a[900],t,p,min;clock_t start,end;

double cpu;

/* BUBBLE SORT */

for(i=0;i<size;i++) a[i]=rand()%100;start=clock(); for(i=0;i<size;i++) { for(j=0;j<size;j++) { if(a[j+1]<a[j]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } }end=clock();cpu=((double)(end-start)/CLOCKS_PER_SEC);printf("\nBUBBLE SORT ");printf("\nTime is %lf \n",cpu);printf("Sorted data \n"); for(i=0;i<size;i++) printf("%d ",a[i]); /* SELECTION SORT */

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

CSE-1(2150703) 1

Page 2: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

a[i]=rand()%100;start=clock(); for(i=0;i<(size-1);i++) { min=i; for(j=i+1;j<size;j++) { if(a[min]>a[j]) { min=j; } }

if(min!=i) { t=a[i]; a[i]=a[min]; a[min]=t; } }end=clock();printf("\nSELECTION SORT ");cpu=((double)(end-start)/CLOCKS_PER_SEC);printf("\nTime is %lf",cpu);printf("\nSorted data \n"); for(i=0;i<size;i++) printf("%d ",a[i]);

/* INSERTION SORT */

for(i=0;i<size;i++) a[i]=rand()%100;start=clock(); for(i=1;i<size;i++) { p=a[i]; j=i-1; while(j>=0 && a[j]>p) { a[j+1]=a[j]; j=j-1; } a[j+1]=p; }end=clock();printf("\nINSERTION SORT ");cpu=((double)(end-start)/CLOCKS_PER_SEC);printf("\nTime is %lf",cpu);

printf("\nSorted data \n"); for(i=0;i<size;i++) printf("%d ",a[i]); printf("\n");

return 0;}

CSE-1(2150703) 2

Page 3: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

OUTPUT :

CSE-1(2150703) 3

Page 4: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

CSE-1(2150703) 4

Page 5: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 2

AIM : Implementation and Time analysis of factorial program using iterative and recursive method.

Code :

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int fact(int x)

{

double f=x;

if(x!=1)

f*=fact(x-1);

else return 1;

return f;

}

void main()

{

int i,n;

double a,fa=1;

clock_t end,start;

double cpu;

printf("\nEnter a number to find factorial ");

scanf("%d",&n);

start=clock();

CSE-1(2150703) 5

Page 6: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

for(i=n;i>0;i--)

{ fa*=i; }

end=clock();

cpu=((double)(end-start)/CLOCKS_PER_SEC);

printf("\nTime for iterative is %lf",cpu);

printf("\nFactorial of %d is %lf\n",n,fa);

start=clock();

a=fact(n);

end=clock();

cpu=((double)(end-start)/CLOCKS_PER_SEC);

printf("\nTime for recursive is %lf",cpu);

printf("\nFactorial of %d is %lf",n,a);

printf("\n");

}

OUTPUT :

CSE-1(2150703) 6

Page 7: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 3

AIM : Implementation and Time analysis of linear search algorithm.

Code :

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#define size 500

void main()

{

int e,a[500],i,c=0;

clock_t end,start;

double cpu;

printf("enter element to search ");

scanf("%d",&e);

printf("\n\n");

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

{ a[i]=rand()%100; }

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

{ printf("%d ",a[i]); }

printf("\n\nThe found elements are \n");

start=clock();

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

{

if(a[i]==e)

{ printf("%d ",a[i]); c++; }

CSE-1(2150703) 7

Page 8: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

}

end=clock();

cpu=((double)(end=start)/CLOCKS_PER_SEC);

printf("\n\nThe number of elements found are %d ",c);

printf("\n\nThe time taken is %lf\n\n",cpu);

}

CSE-1(2150703) 8

Page 9: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 4

AIM : Implementation and Time analysis of binary search algorithm.

Code :

#include <stdio.h>

#include <stdlib.h>

#include<math.h>

#include<time.h>

#define size 200

void sear(int a[],int ,int, int);

int main()

{

clock_t end,start;

double cpu;

int a[200],i,t,j,p;

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

{

a[i]=rand()%100;

}

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

{

for(j=0;j<size;j++)

{

if(a[j]>a[j+1])

{

t=a[j];

a[j]=a[j+1];

CSE-1(2150703) 9

Page 10: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

a[j+1]=t;

}

}

}

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

{

printf("%d ",a[i]);

}

printf("\n\nEnter number to search ");

scanf("%d",&p);

start=clock();

sear(a,p,0,size);

end=clock();

cpu=((double)(end-start)/CLOCKS_PER_SEC);

printf("\nTime = %lf ",cpu);

return 0;

}

void sear(int x[], int n, int a, int b)

{

int z=((a+b)/2);

if(x[z]==n)

{

printf("\nnumber found \n");

}

else if (x[z]>n)

{

sear(x,n,a,z-1);

}

else if (x[z]<n)

{

CSE-1(2150703) 10

Page 11: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

sear(x,n,z+1,b);

}

else

printf("not found");

}

OUTPUT :

CSE-1(2150703) 11

Page 12: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 5

AIM : Implementation and Time analysis of Merge sort and Quick sort algorithms.

Code : Merge sort

#include<stdio.h>

#include<time.h>

#define size 500

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++;

CSE-1(2150703) 12

Page 13: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

}

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);

CSE-1(2150703) 13

Page 14: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

}

}

void main()

{

int arr[size],i;

clock_t start,end;

double cpu;

printf("\n\n");

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

{

arr[i]=rand()%100;

printf("%d ", arr[i]);

}

start=clock();

mergeSort(arr, 0, size - 1);

end=clock();

cpu=((double)(end-start)/CLOCKS_PER_SEC);

printf("\n");

printf("\nSorted array is \n\n");

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

{

printf("%d ",arr[i]);

}

printf("\n");

printf("\nTime is %lf\n",cpu);

}

CSE-1(2150703) 14

Page 15: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

Code : Quick sort

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#define size 100

int a[size];

void quick(int a[],int , int );

int partition(int a[], int , int );

void main()

{

int i;

clock_t start,end;

double cpu;

printf("\nData before sorting\n\n");

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

{

a[i]=rand()%100;

}

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

{

printf("%d ",a[i]);

}

start=clock();

quick(a,0,size-1);

end=clock();

printf("\n\nData after sorting\n\n");

CSE-1(2150703) 15

Page 16: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

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

{

printf("%d ",a[i]);

}

cpu=((double)(end-start)/CLOCKS_PER_SEC);

printf("\n\nTime taken is %lf\n",cpu);

printf("\n\n");

}

void quick(int a[],int le, int ri)

{

int q;

if(le<ri)

{

q=partition(a,le,ri);

quick(a,le,q-1);

quick(a,q+1,ri);

}

}

int partition(int a[], int l, int r)

{

int pivot=a[l];

int lo=l+1;

int hi=r;

while(hi>=lo)

{

while(a[hi]>pivot)

hi-=1;

CSE-1(2150703) 16

Page 17: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

while(a[lo]<=pivot && lo<=hi)

lo+=1;

if(hi>=lo)

{

int k=a[lo];

a[lo]=a[hi];

a[hi]=k;

lo+=1;

hi-=1;

}

}

int p=a[l];

a[l]=a[hi];

a[hi]=p;

return hi;

}

CSE-1(2150703) 17

Page 18: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 6

AIM : Implementation of max-heap sort algorithm.

Code :

#include<stdio.h>

#include<stdlib.h>

int size;

void max_heapify(int *,int );

void build_max(int *);

void heapsort(int *);

int heap_size;

void main()

{

printf("Enter size of array\n");

scanf("%d",&size);

int a[size],i,j,p,k=size-1;

printf("Enter %d elements in array\n",size);

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

scanf("%d",&a[i]);

heapsort(a);

printf("\n\n");

printf("Sorted heap is\n");

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

printf("%d ",a[i]);

printf("\n\n");

CSE-1(2150703) 18

Page 19: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

int y=log2 (size+1)+1,l=0;

for(j=0;j<=log2 (size+1)+1;j++)

{

printf("\n");

for(p=y;p>=0;p--)

printf(" ");

y--;

for(i=0;(i<pow(2,j) && l<size);i++)

{

printf("%d ",a[k]);

k--;

l++;

}

printf("\n");

}

}

void heapsort(int *b)

{

int i,p;

build_max(b);

for(i=size;i>=0;i--)

{

p=b[0];

b[0]=b[i];

b[i]=p;

heap_size--;

max_heapify(b,0);

}

}

CSE-1(2150703) 19

Page 20: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

void build_max(int *c)

{

int i;

heap_size=size;

for(i=size/2;i>=0;i--)

{

max_heapify(c,i);

}

}

void max_heapify(int *d,int a)

{

int lt,rt,largest,p;

lt=a*2;

rt=((a*2)+1);

if(lt<=heap_size && d[lt]>d[a])

largest=lt;

else

largest=a;

if(rt<=heap_size && d[rt]>d[largest])

largest=rt;

if(largest!=a)

{

p=d[a];

d[a]=d[largest];

d[largest]=p;

max_heapify(d,largest);

}

CSE-1(2150703) 20

Page 21: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 7

AIM : Implementation of a knapsack problem using dynamic programming.

Code :

#include<stdio.h>

#include<stdlib.h>

int W,n;

void main()

{

int w,i,l=0,p,j;

printf("Enter number of objects \n");

scanf("%d",&n);

printf("Enter maximum weight, knapsack can carry \n");

scanf("%d",&W);

int k[n+1][W+1],a[W],v[W],ks[W];

printf("Enter weights of objects \n");

a[0]=0;

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

{

printf("w[%d] = ",i);

scanf("%d",&a[i]);

}

printf("Enter profits/values of objects \n");

v[0]=0;

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

{

printf("p[%d] = ",i);

CSE-1(2150703) 21

Page 22: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

scanf("%d",&v[i]);

}

k[0][0]=0;

for(w=1;w<=W;w++)

k[0][w]=0;

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

k[i][0]=0;

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

{

for(w=1;w<=W;w++)

{

if(a[i]<=w)

{

if( v[i] + k[i-1][w-a[i]] > k[i-1][w])

{

k[i][w]= v[i] + k[i-1][w-a[i]];

}

else

{

k[i][w]= k[i-1][w];

}

}

else

{

k[i][w]= k[i-1][w];

}

}

}

printf("\n\nP W i \t0 ");

for(i=1;i<=W;i++)

CSE-1(2150703) 22

Page 23: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

{

printf("%d ",i);

}

printf("\n_________________________________________________________\n");

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

{

printf("%d %d %d |\t",v[i],a[i],i);

for(j=0;j<=W;j++)

{

printf("%d ",k[i][j]);

}

printf("\n");

}

i=n;

p=W;

int u=0,x=0;

while(i>=0 && p>=0)

{

if(k[i][W] != k[i-1][W])

{

ks[++l]=i;

p=p-a[i];

u=u+i;

x+=v[i];

i=i-1;

}

else

i=i-1;

}

CSE-1(2150703) 23

Page 24: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

ks[++l]='\0';

l=1;

printf("\nThe individual weights are ");

while(ks[l]!='\0')

{

printf("%d ",ks[l]);

l++;

}

printf("\n\nTotal weight to be carried in knapsack is %d \n\n",u);

printf("Total profit in knapsack is %d \n\n",x);

}

CSE-1(2150703) 24

Page 25: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 8

AIM : Implementation of chain matrix multiplication using dynamic programming.

Code :

#include <stdio.h>

#include <stdlib.h>

void sel(int arr[5][5],int , int );

int n,i,j,s[10][10];

int main()

{

int l,k,q;

printf("Enter number of matrices\n");

scanf("%d",&n);

int p[n+1],m[n+1][n+1];

printf("\nEnter P array values\n");

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

{

printf("p[%d] = ",i);

scanf("%d",&p[i]);

}

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

{

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

{

s[i][j]=0; m[i][j]=0;

CSE-1(2150703) 25

Page 26: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

}

}

for(l=2;l<n+1;l++)

{

for(i=1;i<n-l+2;i++)

{

j=i+l-1;

m[i][j]=100000;

for(k=i;k<j;k++)

{

q=m[i][k]+m[k+1][j]+((p[i-1])*(p[k])*(p[j]));

if(q<m[i][j])

{

m[i][j]=q;

s[i][j]=k;

}

}

}

}

printf("\n\nm Table\n\n");

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

{

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

{

printf("%d\t",m[i][j]);

}

printf("\n");

}

printf("\n\n\ns Table\n\n");

CSE-1(2150703) 26

Page 27: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

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

{

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

{

printf("%d\t",s[i][j]);

}

printf("\n");

}

printf("\n\nThe solution is : ");

sel(s,1,n);

printf("\n\n");

return 0;

}

void sel(int s1[5][5],int i, int j)

{

int a,b;

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

{

for(b=0;b<n+1;b++)

{

s1[a][b]=s[a][b];

}

}

if(i==j)

{

printf("A%d",i);return;

}

else

{

CSE-1(2150703) 27

Page 28: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

printf(" ( ");

sel(s1,i,s1[i][j]);

sel(s1,(1+s1[i][j]),j);

printf(" ) ");

}

}

CSE-1(2150703) 28

Page 29: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 9

AIM : Implementation of making a change problem using dynamic programming.

Code :

#include <stdio.h>

#include <stdlib.h>

int main()

{

int m,n,i,j;

printf("Enter the amount of which you want change\n");

scanf("%d",&m);

printf("\nEnter the number of determinants\n");

scanf("%d",&n);

int c[n+1][m+1],dc[n+1];

printf("\nEnter the determinants\n");

dc[0]=0;

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

scanf("%d",&dc[i]);

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

{

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

c[i][j]=0;

}

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

c[i][0]=0;

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

{

CSE-1(2150703) 29

Page 30: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

c[0][i]=i;

}

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

{

for(j=1;j<m+1;j++)

{

if(dc[1]>c[0][j])

c[i][j]=0;

else if(dc[i]<=j)

{

if((c[i][j-dc[i]] +1 )<c[i-1][j])

c[i][j]=( c[i][j-dc[i]] +1 );

else

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

}

else

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

}

}

printf("\n");

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

{

printf("%d ",dc[i]);

for(j=0;j<m+1;j++)

{

printf(" %d",c[i][j]);

}

printf("\n");

}

printf("\nThe change you get is \n");

CSE-1(2150703) 30

Page 31: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

i=n; j=m;

while(i!=1)

{

if(c[i][j]==c[i-1][j])

i=i-1;

else

{

j=j-dc[i];

printf(" %d ",dc[i]);

}

}

printf("\n\n");

return 0;

}

CSE-1(2150703) 31

Page 32: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 10

AIM : Implementation of knapsack using greedy algorithm.

Code :

#include <stdio.h>

#include <stdlib.h>

int main()

{

int m,n,i,j;

printf("Enter maximum weight of knapsack ");

scanf("%d",&m);

printf("\nEnter number of objects ");

scanf("%d",&n);

int wt=0,k=0;

float cal[n],p[n],w[n],x[n],prof=0;

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

x[i]=0;

printf("\nEnter weights\n");

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

{

printf("w[%d] = ",i);

scanf("%f",&w[i]);

}

printf("\nEnter profits\n");

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

{

printf("p[%d] = ",i);

scanf("%f",&p[i]);

CSE-1(2150703) 32

Page 33: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

}

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

cal[i]=p[i]/w[i];

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

{

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

{

if(cal[i]<cal[j])

{

int t1,t2,t3;

t1=cal[i]; cal[i]=cal[j]; cal[j]=t1;

t2=w[i]; w[i]=w[j]; w[j]=t2;

t3=p[i]; p[i]=p[j]; p[j]=t3;

}

}

}

printf("\n\n p[i]\t\t w[i]\t\t cal[i]\n");

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

printf("%f\t %f\t %f\t\n",p[i],w[i],cal[i]);

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

{

if((wt+w[i])<=m)

{

k++; x[i]=1; wt+=w[i]; prof+=p[i];

}

else

{

k++; x[i]=(m-wt)/w[i]; w[i]=m-wt; wt=m; prof+=(x[i]*p[i]); p[i]=(x[i]*p[i]); break;

}

CSE-1(2150703) 33

Page 34: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

}

printf("\nThe selected weights are \n\ni\t w[i]\t\t p[i]\n");

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

printf("%d\t%f\t%f\n",i+1,w[i],p[i]);

printf("\n\nThe total profit is %f\n\n",prof);

return 0;

}

CSE-1(2150703) 34

Page 35: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 11

AIM : Implementation of Graph and Searching using DFS.

Code :

#include<stdio.h>

#include<conio.h>

int a[20][20],reach[20],n;

void dfs(int v)

{

int i;

reach[v]=1;

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

{

if(a[v][i] && !reach[i])

{

printf("\n %d->%d",v,i);

dfs(i);

}

}

}

void main()

{

int i,j,count=0;printf("\n Enter number of vertices");

scanf("%d",&n);

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

{

reach[i]=0;

CSE-1(2150703) 35

Page 36: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

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

a[i][j]=0;

}

printf("\nEnter adjacency matrix:\n");

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

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

scanf("%d",&a[i][j]);

dfs(1);

printf("\n");

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

{ if(reach[i])

count++;

}

if(count==n)

printf("\n Graph is connected");

else

printf("\n Graph is not connected");

getch();

}

CSE-1(2150703) 36

Page 37: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 12

AIM : Implementation of Graph and Searching using BFS.

Code :

#include<stdio.h>

#include<conio.h>

int a[20][20],visited[20],n,i,j,f=0,r=-1,q[20];

void bfs(int v)

{

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

{

if(a[v][i] && !visited[i])

q[++r]=i;

if(f<=r)

{

visited[q[f]]=1;

bfs(q[f++]);

}

}

}

void main()

{

int v;

printf("\n Enter number of vertices");

scanf("%d",&n);

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

{

q[i]=0;

CSE-1(2150703) 37

Page 38: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

visited[i]=0;

}

printf("\nEnter adjacency matrix:\n");

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

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

scanf("%d",&a[i][j]);

printf("Enter starting vertex:");

scanf("%d",&v);

bfs(v);

printf("\n The node which are reachable are:\n");

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

if(visited[i])

printf("%d\t",i);

else

printf("\n bfs is not possible");

getch();

}

CSE-1(2150703) 38

Page 39: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 13

AIM : Implement prim’s algorithm.

Code :

#include<iostream>

using namespace std;

int main()

{

int n;char c='a';

cout<<"enter number of nodes";

cin>>n;

int g[n][n];

int stree[n][3];

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

stree[i][0]=stree[i][1]=stree[i][2]=-1;

cout<<"enter graph egdes 0 for no egde and weight for edge\n";

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

{

for(int j=i;j<n;j++)

{ g[i][j]=0;

if(i!=j)

{

cout<<"("<<((char)(c+i))<<","<<(char)(c+j)<<"):-";

cin>>g[i][j];

g[j][i]=g[i][j];

}

}

}

CSE-1(2150703) 39

Page 40: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

cout<<"\t ";

for(int i=0;i<n;i++)cout<<(char)(c+i)<<"\t";

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

{

cout<<endl<<(char)(c+i)<<"\t ";

for(int j=0;j<n;j++)

{

if(g[i][j]!=0) cout<<"|"<<g[i][j];

else cout<<"|-";

cout<<"\t";

}

}

//root node

stree[0][0]=stree[0][1]=0;

stree[0][2]=1;

//making stree

int counter=0,i=0;

while(counter<n)

{

for(int k=0;k<n;k++)

{

if(g[i][k]!=0)

{

if(stree[k][0]==-1&&stree[k][2]!=1)

{

stree[k][0]=i;

stree[k][1]=g[i][k];

}

else

{

CSE-1(2150703) 40

Page 41: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

if(stree[k][1]>g[i][k]&&stree[k][2]!=1)

{

stree[k][0]=i;

stree[k][1]=g[i][k];

}

}

}

}

int temp=-1;

for(int k=0;k<n;k++)

{

if(stree[k][2]!=1 && stree[k][0]!=-1)

{

if(temp==-1)

temp=k;

else

{

if(stree[temp][1]>stree[k][1])

temp=k;

}

}

}

i=temp;

stree[i][2]=1;counter++;

}

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

cout<<endl<<(char)(c+i)<<":-"<<(char)(c+stree[i][0])<<","<<stree[i][1];

}

CSE-1(2150703) 41

Page 42: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 14

AIM : Implement kruskal’s algorithm.

Code :

#include<iostream>

using namespace std;

class edge

{

public: int w=0,e1=0,e2=0;

};

int main()

{

int n;char c='a';int k=0;

cout<<"enter number of nodes";

cin>>n;

int g[n][n];

int stree[n][n],snode[n];

edge e[n*n];

//defalut values for tree

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

stree[i][0]=stree[i][1]=stree[i][2]=-1;

cout<<"enter graph egdes 0 for no egde and weight for edge\n";

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

for(int j=i;j<n;j++){

g[i][j]=0;

if(i!=j){

cout<<"("<<((char)(c+i))<<","<<(char)(c+j)<<"):-";

cin>>g[i][j];

CSE-1(2150703) 42

Page 43: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

g[j][i]=g[i][j];

if(g[i][j]!=0){

e[k].w=g[i][j];

e[k].e1=i;

e[k++].e2=j;

} } } }

//printing graph table

cout<<"\t ";

for(int i=0;i<n;i++) cout<<(char)(c+i)<<"\t";

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

cout<<endl<<(char)(c+i)<<"\t ";

for(int j=0;j<n;j++){

if(g[i][j]!=0) cout<<"|"<<g[i][j];

else cout<<"|-";

cout<<"\t";

}

}

//sorting

for(int i=0;i<k;i++){

for(int j=k-1;j>i;j--){

if(e[i].w>e[j].w){

edge tem;

tem=e[i];

e[i]=e[j];

e[j]=tem;

} } }

//printing sorted edges

for(int i=0;i<k;i++)

cout<<endl<<e[i].w<<"("<<(char)(c+e[i].e1)<<","<<(char)(c+e[i].e2)<<")";

//make graph-tree table;

CSE-1(2150703) 43

Page 44: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

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

snode[i]=-1;

for(int j=0;j<n;j++)

stree[i][j]=-1;

}

int gp=0;

for(int i=0;i<k;i++){

if((snode[e[i].e1]==-1&&snode[e[i].e2]==-1)){

stree[e[i].e1][e[i].e2]=stree[e[i].e2][e[i].e1]=e[i].w;

snode[e[i].e1]=snode[e[i].e2]=gp;

gp++;

}

else if(stree[e[i].e1][e[i].e2]==-1&&(snode[e[i].e1]!=snode[e[i].e2])){

stree[e[i].e1][e[i].e2]=stree[e[i].e2][e[i].e1]=e[i].w;

if(snode[e[i].e1]!=-1&&snode[e[i].e2]==-1){

snode[e[i].e2]=snode[e[i].e1];

}

else if(snode[e[i].e2]!=-1&&snode[e[i].e1]==-1){

snode[e[i].e1]=snode[e[i].e2];

}

else{

int temp=snode[e[i].e2];

for(int l=0;l<n;l++){

if(snode[l]==temp){

snode[l]=snode[e[i].e1];

} } } } }

cout<<endl<<"\t ";

for(int i=0;i<n;i++) cout<<(char)(c+i)<<"\t ";

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

cout<<endl<<(char)(c+i)<<"\t";

CSE-1(2150703) 44

Page 45: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

for(int j=0;j<n;j++){

if(stree[i][j]!=-1) cout<<"|"<<stree[i][j];

else cout<<"|-";

cout<<"\t";

} } }

CSE-1(2150703) 45

Page 46: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

PRACTICAL – 15

AIM : Implement LCS problem.

Code :

#include<stdio.h>

void lcs(int arr[][10], char ar[], int , int );

int l,k;

void main()

{

char s1[10],s2[10];

int i,j;

printf("\nEnter the strings\nX = ");

s1[0]=0;

char ch=getchar();

while(ch!='\n')

{

s1[++l]=ch;

ch=getchar();

}

printf("Y = ");

s2[0]=0;

char c1=getchar();

while(c1!='\n')

{

s2[++k]=c1;

c1=getchar();

}

CSE-1(2150703) 46

Page 47: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

int c[l+1][k+1],b[l+1][k+1];

for(i=0;i<l+1;i++)

{

for(j=0;j<k+1;j++)

{

b[i][j]=0; c[i][j]=0;

}

}

//1=diagonal 2=up 3=left

for(i=1;i<l+1;i++)

{

for(j=1;j<k+1;j++)

{

if(s1[i]==s2[j])

{

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

b[i][j]=1;//diagonal

}

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

{

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

b[i][j]=2;//up

}

else

{

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

b[i][j]=3;//left

}

}

}

CSE-1(2150703) 47

Page 48: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

printf("\n\n\t");

for(i=1;i<k+1;i++)

{

printf("\t %c",s2[i]);

}

printf("\n");

for(i=0;i<l+1;i++)

{

printf("%c\t",s1[i]);

for(j=0;j<k+1;j++)

{

printf("%d(%d)\t",c[i][j],b[i][j]);

}

printf("\n");

}

printf("\n\n");

printf("Longest common substring of X and Y is ");

lcs(b,s1,l,k);

printf("\n\n");

}

void lcs(int arr[l+1][k+1], char ar[l+1], int a, int p)

{

if(a==0 || p==0)

return;

if(arr[a][p]==1)

{

lcs(arr,ar,a-1,p-1);

printf("%c ",ar[a]);

}

CSE-1(2150703) 48

Page 49: thesupplier4u.files.wordpress.com · Web viewImplementation and time analysis of sorting algorithms. Bubble sort, Selection sort, Insertion sort

XXXXXXXXXXXX

else if(arr[a][p]==2)

{

lcs(arr,ar,a-1,p);

}

else if(arr[a][p]==3)

{

lcs(arr,ar,a,p-1);

}

}

CSE-1(2150703) 49