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

DAA Lab Programms

The document contains 10 programs related to searching and sorting algorithms in C language. It includes programs for linear search, binary search, insertion sort, selection sort, quick sort, merge sort, knapsack problem, Kruskal's algorithm, Floyd Warshall's algorithm and Dijkstra's algorithm. Each program section contains the C code for implementing the algorithm, sample input/output and brief description.

Uploaded by

lokesh_045
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
353 views

DAA Lab Programms

The document contains 10 programs related to searching and sorting algorithms in C language. It includes programs for linear search, binary search, insertion sort, selection sort, quick sort, merge sort, knapsack problem, Kruskal's algorithm, Floyd Warshall's algorithm and Dijkstra's algorithm. Each program section contains the C code for implementing the algorithm, sample input/output and brief description.

Uploaded by

lokesh_045
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

INDEX

S.NO. CONTENTS

DATE

SIGN.

1. 2. 3. 4. 5. 6. 7.

Program for Linear search in C. Program for Binary search in C. Program for Insertion Sort in C. Program for Selection Sort in C. Program for Quick Sort in C. Program for Merge Sort in C.

Program to Implement knapsack problem in C. 8. Program to Implement Kruskals Algorithm in C. 9. Program to Implement Floyd Warshalls algorithm in C. 10. Program to Implement Dijakstras algorithm in C.

1.Write a Program for Linear search in C. #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in array\n"); scanf("%d",&n); printf("Enter %d integer(s)\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to search\n"); scanf("%d", &search); for (c = 0; c < n; c++) { if (array[c] == search) /* if required element found */ { printf("%d is present at location %d.\n", search, c+1); break; } } if (c == n) printf("%d is not present in array.\n", search); return 0; }

OUTPUT Enter the number of elements in array 5 Enter 5 numbers 5 6 4 2 9 Enter the number to search 4 4 is present at location 3.

2.Write a Program for Binary search in C. #include <stdio.h> int 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);
4

return 0; }

Output Enter number of elements 7 Enter 7 integers -4 5 8 9 11 43 485 Enter value to find 11 11 found at location 5.

3.Write a Program for Insertion Sort in C. #include <stdio.h> int main() { int n, array[1000], c, d, t; 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++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:\n"); for (c = 0; c <= n - 1; c++) { printf("%d\n", array[c]); } return 0; }

OUTPUT Enter number of elements 5 Enter 5 integers 5 45 96 85 9 Sorted list in ascending order : 5 9 45 85 96

4.Write a Program for Selection Sort in C. #include <stdi o.h> int 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]); return 0; }
8

OUTPUT Enter number of elements 5 Enter 5 integers 15 456 96 18 9 Sorted list in ascending order : 9 15 18 96 456

5.Write a Program for Quick Sort in C. #include<stdio.h> void quicksort(int [10],int,int); int main(){ int x[20],size,i; printf("Enter size of the array: "); scanf("%d",&size); printf("Enter %d elements: ",size); for(i=0;i<size;i++) scanf("%d",&x[i]); quicksort(x,0,size-1); printf("Sorted elements: "); for(i=0;i<size;i++) printf(" %d",x[i]); return 0; } void quicksort(int x[10],int first,int last){ int pivot,j,temp,i; if(first<last){ pivot=first; i=first; j=last; while(i<j){ while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j){ temp=x[i];
10

x[i]=x[j]; x[j]=temp; } } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quicksort(x,first,j-1); quicksort(x,j+1,last); } }

OUTPUT Enter size of the array : 5 Enter 5 elements : 5 23 2 96 44 Sorted list is : 2 5 23 44 96

11

6.Write a Program for Merge Sort in C. #include<stdio.h> #define MAX 50 void mergeSort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main(){ int merge[MAX],i,n; printf("Enter the total number of elements: "); scanf("%d",&n); printf("Enter the elements which to be sort: "); for(i=0;i<n;i++){ scanf("%d",&merge[i]); } partition(merge,0,n-1); printf("After merge sorting elements are: "); for(i=0;i<n;i++){ printf("%d ",merge[i]); } return 0; } void partition(int arr[],int low,int high){ int mid; if(low<high){ mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergeSort(arr,low,mid,high); }
12

} void mergeSort(int arr[],int low,int mid,int high){ int i,m,k,l,temp[MAX]; l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high)){ if(arr[l]<=arr[m]){ temp[i]=arr[l]; l++; } else{ temp[i]=arr[m]; m++; } i++; } if(l>mid){ for(k=m;k<=high;k++){ temp[i]=arr[k]; i++; } } else{ for(k=l;k<=mid;k++){ temp[i]=arr[k]; i++; } } for(k=low;k<=high;k++){ arr[k]=temp[k]; } }
13

OUTPUT Enter the total no. of elements: 4 Enter the element which to be sort: 4 5 6 1 After merge sorting, elements are: 1 4 5 6

14

7.Write a Program to Implement knapsack problem in C. # 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("n The result vector is:- "); for(i=0;i<n;i++) printf("%ft",x[i]); printf("m Maximum profit is:- %f", tp); } void main() { float weight[20], profit[20], capacity; int n, i ,j; float ratio[20], temp; clrscr(); printf ("n Enter the no. of objects:- "); scanf ("%d", &num); printf ("n Enter the weights and profits of each object:- ");
15

for (i=0; i<n; i++) { scanf("%f %f", &weight[i], &profit[i]); } printf ("n enter the capacity of knapsack:- "); scanf ("%f", &capacity); for (i=0; i<n; i++) { ratio[i]=profit[i]/weight[i]; } for(i=0; i<n; i++) { for(j=i+1;j< n; j++) { if(ratio[i]<ratio[j]) { temp= ratio[j]; ratio[j]= ratio[i]; ratio[i]= temp; temp= weight[j]; weight[j]= weight[i]; weight[i]= temp; temp= profit[j]; profit[j]= profit[i]; profit[i]= temp; } } } knapsack(n, weight, profit, capacity); getch(); }

16

OUTPUT Enter the number of objects :Enter the weights and profits of each object :Enter the capacity of knapsack :The result vector is :Maximum profit is :-

Enter the no. of objects:3 Enter the weight and profit of each object:56 45 34 Enter the capacity of knapsack:7 The result vector is:3 2 Maximum profit is:-9

17

8.Write a Program to Implement Kruskals Algorithm in C. #include<stdio.h> #include<conio.h> #include<stdlib.h> int i,j,k,a,b,u,v,n,ne=1; int min,mincost=0,cost[9][9],parent[9]; int find(int); int uni(int,int); void main() { clrscr(); printf("\n\n\tImplementation of Kruskal's algorithm\n\n"); printf("\nEnter the no. of vertices\n"); scanf("%d",&n); printf("\nEnter the cost adjacency matrix\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&cost[i][j]); if(cost[i][j]==0) cost[i][j]=999; } } printf("\nThe edges of Minimum Cost Spanning Tree are\n\n"); while(ne<n) { for(i=1,min=999;i<=n;i++) { for(j=1;j<=n;j++) { if(cost[i][j]<min) { min=cost[i][j]; a=u=i; b=v=j; } } }
18

u=find(u); v=find(v); if(uni(u,v)) { printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min); mincost +=min; } cost[a][b]=cost[b][a]=999; } printf("\n\tMinimum cost = %d\n",mincost); getch(); } int find(int i) { while(parent[i]) i=parent[i]; return i; } int uni(int i,int j) { if(i!=j) { parent[j]=i; return 1; } return 0; }

19

OUTPUT Implementaion of Kruskals' algorithm Enter the no. of vertices: 4 Enter the cost adjacency matrix: 4 5 8 7 2 1 6 7 9 2 5 6 2 7 8 6 The edges of minimum cost spanning Tree are 1 edge (2,1) =2 2 edge (3,2)=2 3 edge (4,1)=2 Minimum Cost =6

20

9.Write a Program to Implement Floyd Warshalls algorithm in C. #include<Stdio.h> #include<Conio.h> int max(int,int); void warshal(int p[10][10],int n) { int i,j,k; for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) p[i][j]=max(p[i][j],p[i][k]&&p[k][j]); } int max(int a,int b) { if(a>b) return(a); else return(b); } void main() { int p[10][10]={0},n,e,u,v,i,j; clrscr(); printf("\n Enter the number of vertices:"); scanf("%d",&n); printf("\n Enter the number of edges:"); scanf("%d",&e); for(i=1;i<=e;i++) { printf("\n Enter the end vertices of edges%d:",i); scanf("%d%d",&u,&v); p[u][v]=1; } printf("\n Matrix of Input Data:\n");
21

for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%d\t",p[i][j]); printf("\n"); } warshal(p,n); printf("\n Traversitive Closure:\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) printf("%d\t",p[i][j]); printf("\n"); } getch(); }

22

OUTPUT WARSHALS ALGORITHM Enter the number of verices:3 Enter the number of edges:3 Enter the end vertices of edges1: 1 2 Enter the end vertices of edges2: 2 3 Enter the end vertices of edges3: 1 3 Matrix of Input Data: 011 001 000 Ttraversitive Closure: 011 001 000

23

10.Write a Program to Implement Dijakstras algorithm in C. #include<Stdio.h> #include<Conio.h> int n; int weight[100][100]; char intree[100]; int d[100]; int whoTo[100]; void updatedistance(int target) { int i; for(i=0;i<n;i++) if((weight[target][i]!=0)&&(d[i]>weight[target][i])) { d[i]=weight[target][i]; whoTo[i]=target; } } void main() { FILE *f=fopen("dist.txt","r"); int i,j,total,treesize; clrscr(); fscanf(f,"%d",&n); for(i=0;i for(j=0;j fscanf(f,"%d",&weight[i][j]); fclose(f); for(i=0;i intree[i]=0; printf("Adding Nodes:%c\n",0+'A'); intree[0]=1; updatedistance(0); total=1;
24

for(treesize=1;treesize { int min=-1; for(i=0;i<n;i++) if(!intree[i]) if((min==-1)||(d[min]>d[i])) min=i; printf("Adding Edge%c-%c\n",whoTo[min]+'A',min+'A'); intree[min]=1; total+=d[min]; updatedistance(min); } printf("Total Distance:%d \n",total); getch(); }

OUTPUT: DIJIKSTRAS ALGORITHM Adding Nodes: A Total Distance: 1

25

You might also like