Sei sulla pagina 1di 23

S.No.

1 2 3 4 5 6 7 8 9 10 11 12 13

Topic Writeaprogramtosortlistofnumbersusingbubblesort Writeaprogramthatimplementsinsertionsort Writeaprogramthatimplementsquicksort Writeaprogramthatimplementsmerge sort Writeaprogramthatimplementsbinarysearch WriteaprogramthatimplementsDepthfirstsearch WriteaprogramthatimplementsBreadth first search WriteaprogramthatimplementsPrimsalgorithm. WriteaprogramthatimplementsKruskalsalgorithm WriteaprogramthatimplementsDijkstrasaalgorithm. Writeaprogramthatfindmatrixmultiplication Write aprogramthatimplementsknapsackusinggreedy.

Page No. 1-2 4 5-6 78 9 10 11 12 13 14 15 16 18 19 20 21 22

Remarks

Writeaprogramtosortlistofnumbersusingselectionsort 3

Q1 Writeaprogramtosortlistofnumbersusingbubblesort

#include<stdio.h> void main() { int arr[50],temp,i,j,n; printf("\nEnter any Value less Than 50"); scanf("%d",&n); printf("\n\tEnter The Values into ARRAY "); for(i=0;i<n;i++) { printf("\n\n Enter Element no %d: ",i+1); scanf("%d",&arr[i]); } for(i=1;i<n;i++) { for(j=0;j<n-i;j++) { if(arr[j] >arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } printf("\n\n-- Sorted Series --"); for(i=0;i<n;i++) { printf("\n %d",arr[i]); } }

Page 1

Output

Page 2

Q2 Writeaprogramtosortlistofnumbersusingselectionsort ?
#include<stdio.h> void main() { int array[100], n, c, d, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } } printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); }

Page 3

Q3 Writeaprogramthatimplementsinsertionsort
#include<stdio.h> void main() { int array[100], n, c, d, swap, k; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); for ( c = 1 ; c <= n - 1 ; c++ ) { for ( d = 0 ; d <= c - 1 ; d++ ) { if ( array[c] < array[d] ) { swap = array[d]; array[d] = array[c]; for ( k = c ; k > d ; k-- ) array[k] = array[k-1]; array[k+1] = swap; } } }

printf("Sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); }

Page 4

Q4 Writeaprogramthatimplementsquicksort ?
#include<stdio.h> int split(int [],int,int); void quicksort(int [],int,int); void main() { int arr[20],n,i; printf("\nQUICk SORT\n"); printf("Enter the no.of elements:"); scanf("%d",&n); printf("Enter the elements:"); for(i=0;i<n;i++) scanf("%d",&arr[i]); printf("\nArray before sorting:\n"); for(i=0;i<n;i++) printf("%d\t",arr[i]); quicksort(arr,0,n); printf("\nArray after sorting:\n"); for(i=0;i<n;i++) printf("%d\t",arr[i]); } void quicksort(int a[],int lower,int upper) { int i; if(upper>lower) { i=split(a,lower,upper); quicksort(a,lower,i-1); quicksort(a,i+1,upper); } } int split(int a[],int lower,int upper) { int i,p,q,t; p=lower+1;

Page 5

q=upper; i=a[lower]; while(q>=p) { while(a[p]<i) p++; while(a[q]>i) q--; if(q>p) { t=a[p]; a[p]=a[q]; a[q]=t; } } t=a[lower]; a[lower]=a[q]; a[q]=t; return(q); }

Page 6

Q5 Writeaprogramthatimplementsmerge sort ?
#include <stdio.h> #include <stdlib.h> #define MAXARRAY 100 void mergesort(int a[], int low, int high); void main(void) { int array[MAXARRAY],n; int i = 0; /* reading the elements form the users*/ printf("Enter the number of elements to be sorted:"); scanf("%d",&n); printf("Enter the elements to be sorted:\n"); for(i = 0; i < n; i++ ){ scanf("%d",&array[i]); } /* array before mergesort */ printf("Before sorting:"); for(i = 0; i < n; i++) printf(" %d", array[i]); printf("\n"); mergesort(array, 0, n - 1); /* array after mergesort */ printf("After Mergesort:"); for(i = 0; i < n; i++) printf(" %d", array[i]); printf("\n"); } void mergesort(int a[], int low, int high) {

Page 7

int int int int int int

i = 0; length = high - low + 1; pivot = 0; merge1 = 0; merge2 = 0; working[length];

if(low == high) return; pivot = (low + high) / 2;

mergesort(a, low, pivot); mergesort(a, pivot + 1, high); for(i = 0; i < length; i++) working[i] = a[low + i]; merge1 = 0; merge2 = pivot - low + 1; for(i = 0; i < length; i++) { if(merge2 <= high - low) if(merge1 <= pivot - low) if(working[merge1] > working[merge2]) a[i + low] = working[merge2++]; else a[i + low] = working[merge1++]; else a[i + low] = working[merge2++]; else a[i + low] = working[merge1++]; } }

Page 8

Q6 Writeaprogramthatimplementsbinarysearch
#include<stdio.h> void main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if ( first > last ) printf("Not found! %d is not present in the list.\n", search); }

Page 9

Q7 WriteaprogramthatimplementsDepthfirstsearch
#include<stdio.h> #include<stdlib.h> int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10]; void main() { int m; printf("enterno of vertices"); scanf ("%d",&n); printf("ente no of edges"); scanf ("%d",&m); printf("\nEDGES \n"); for(k=1;k<=m;k++) { scanf("%d %d",&i,&j); cost[i][j]=1; } printf("enter initial vertex"); scanf("%d",&v); printf("ORDER OF VISITED VERTICES"); printf("%d ",v); visited[v]=1; k=1; while(k<n) { for(j=n;j>=1;j--) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; } v=stk[--top]; printf("%d ",v); k++; visit[v]=0; visited[v]=1; } }

Page 10

Q8 WriteaprogramthatimplementsBreadth first search


#include<stdio.h> #include<stdlib.h> int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10]; void main() { int m; printf("enterno of vertices"); scanf ("%d",&n); printf("ente no of edges"); scanf ("%d",&m); printf("\nEDGES \n"); for(k=1;k<=m;k++) { scanf("%d%d",&i,&j); cost[i][j]=1; } printf("enter initial vertex"); scanf("%d",&v); printf("Visitied vertices\n"); printf ("%d",v); visited[v]=1; k=1; while(k<n) { for(j=1;j<=n;j++) if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1) { visit[j]=1; qu[rare++]=j; } v=qu[front++]; printf("%d ",v); k++; visit[v]=0; visited[v]=1; } }

Page 11

Q9 WriteaprogramthatimplementsPrimsalgorithm.
#include<stdio.h> #include<stdlib.h> int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u; void main() { int m,c; printf ("enterno of vertices"); scanf ("%d", &n); printf ("ente no of edges"); scanf ("%d",&m); printf ("\nEDGES Cost\n"); for(k=1;k<=m;k++) { scanf ("%d%d%d",&i,&j,&c); cost[i][j]=c; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]==0) cost[i][j]=31999; printf ("ORDER OF VISITED VERTICES"); k=1; while(k<n) { m=31999; if(k==1) { for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(cost[i][j]<m) { m=cost[i][j]; u=i; } } else {

Page 12

for(j=n;j>=1;j--) if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; m=cost[v][j]; u=j; } } cost[v][u]=31999; v=u; printf("%d",v); k++; visit[v]=0; visited[v]=1; } }

Page 13

Q10 WriteaprogramthatimplementsKruskalsalgorithm
#include<stdio.h> #include<stdlib.h> int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p; void main() { int dup1,dup2; printf("enter no of vertices"); scanf("%d",&n); printf("enter no of edges"); scanf ("%d",&m); printf ("EDGE Cost"); for(k=1;k<=m;k++) { scanf ("%d%d%d",&i,&j,&c); cost[i][j]=c; cost[j][i]=c; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]==0) cost[i][j]=31999; visit=1; while(visit<n) { v=31999; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]!=31999 && cost[i][j]<v { int count =0; for(p=1;p<=n;p++) { if(visited[p]==i || visited[p]==j) count++; } if(count >= 2) { for(p=1;p<=n;p++) if(cost[i][p]!=31999 && p!=j) dup1=p; for(p=1;p<=n;p++) if(cost[j][p]!=31999 && p!=i) dup2=p; if(cost[dup1][dup2]==-1) continue; } l=i; k=j; v=cost[i][j];

&& cost[i][j]!=-1 )

Page 14

} printf ("edge from %d", l ); printf("--> %d",k); cost[l][k]=-1; cost[k][l]=-1; visit++; int count=0; count1=0; for(i=1;i<=n;i++) { if(visited[i]==l) count++; if(visited[i]==k) count1++; } if(count==0) visited[++vst]=l; if(count1==0) visited[++vst]=k; } }

Page 15

Q11 WriteaprogramthatimplementsDijkstrasaalgorithm.
#include<iostream.h> int path[10]; class dijk { int nodes,cost[10][10],dist[10],s[10],source,dest; public: dijk() { cout<<"enter no. of nodes\n"; cin>>nodes; } void read(); void dijkstras(); void display(); }; void dijk::read() { int i,j,val,edges,count=0; for(i=0;i<nodes;i++) for(j=0;j<nodes;j++) if(i==j) cost[i][j]=0; else cost[i][j]=9999; cout<<"\nenter no. of edges\n"; cin>>edges; while(count<edges) { cout<<"enter Vertex Vi and Vj\n"; cin>>i>>j; cout<<"enter cost along this edge\n "; cin>>val; cost[i][j]=cost[j][i]=val; count++; } } void dijk::dijkstras() { int i,j,v1,v2,min; cout<<"enter source and then destination\n"; cin>>source>>dest; for(i=0;i<nodes;i++) { dist[i]=cost1[i]; s[i]=0;

Page 16

path[i]=source; } s1=1; for(i=1;i<nodes;i++) { min=9999; v1=-1; for(j=0;j<nodes;j++) { if(s[j]==0) if(dist[j]<min) { min=dist[j]; v1=j; } } s[v1]=1; for(v2=0;v2<nodes;v2++) if(s[v2]==0) if(dist[v1]+cost[v1][v2]<dist[v2]) { dist[v2]=dist[v1]+cost[v1][v2]; path[v2]=v1; } } } void dijk::display() { int i; cout<<"Shortest path between "<<source<<" and "<<dest<<" is: \n"; for(i=dest;i!=source;i=path[i]) cout<<i<<"<-"; cout<<source; cout<<"\nthe length="<<dist[dest]; } void main() { dijk d; d.read(); d.dijkstras(); d.display(); }

Page 17

Page 18

Q12 Writeaprogramthatfindmatrixmultiplication
#include<stdio.h> void main() { int arr1[3][3],arr2[3][3]; int arr3[3][3]; int i,j,k; printf("Enter the matrix 1 :"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&arr1[i][j]); } } printf("\nEnter the matrix 2 :"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&arr2[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { arr3[i][j]=0; for(k=0;k<3;k++) { arr3[i][j]=arr3[i][j]+arr1[i][k]*arr2[k][j]; } } } printf("\nResultant matrix is :"); printf("\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("\t%d",arr3[i][j]); } printf("\n"); } }

Page 19

Page 20

Q13 Write aprogramthatimplementsknapsackusinggreedy.


#include<stdio.h> int main() { int array[2][100],n,w,i,curw,used[100],maxi=-1,totalprofit=0; //input number of objects printf("Enter number of objects: "); scanf("%d",&n); //input max weight of knapsack printf("Enter the weight of the knapsack: "); scanf("%d",&w); /* Array's first row is to store weights second row is to store profits */ for(i=0;i<n;i++) { scanf("%d%d",&array[0][i],&array[1][i]); } for(i=0;i<n;i++) { used[i]=0; } curw=w; //loop until knapsack is full while(curw>=0) { maxi=-1; //loop to find max profit object for(i=0;i<n;i++) { if((used[i]==0)&&((maxi==1)||(((float)array[1][i]/(float)array[0][i])>((float)array[1][maxi]/(float)ar ray[0][maxi])))) { maxi=i; } } used[maxi]=1; //decrease current wight curw-=array[0][maxi]; //increase total profit totalprofit+=array[1][maxi]; if(curw>=0) { printf("\nAdded object %d",maxi+1); printf(" Weight: %d",array[0][maxi]); printf(" Profit: %d",array[1][maxi]); printf(" completely in the bag, Space left: %d",curw); } else { printf("\nAdded object %d",maxi+1); printf(" Weight: %d",(array[0][maxi]+curw));

Page 21

printf(" Profit: %d",(array[1][maxi]/array[0][maxi])*(array[0][maxi]+curw)); printf(" partially in the bag, Space left: 0 \n Weight added is: %d",curw+array[0][maxi]); totalprofit-=array[1][maxi]; totalprofit+=((array[1][maxi]/array[0][maxi])*(array[0][maxi]+curw)); } } //print total worth of objects filled in knapsack printf("\nBags filled with objects worth: %d",totalprofit); }

Page 22

Potrebbero piacerti anche