Professional Documents
Culture Documents
Algorithms Laboratory (ADA)
Algorithms Laboratory (ADA)
06CSL58
Perform recursive binary search and linear search and hence find the time required to search an element #include<stdio.h> #include<conio.h> #include<time.h> int binsearch(int a[], int key,int first,int last) { int mid; if(first > last) return -1; else { mid=(first+last)/2; if(key < a[mid]) return binsearch(a,key,first,mid-1); else if(key > a[mid]) return binsearch(a,key,mid+1,last); else return mid; } } int linsearch(int a[10],int i,int n,int key) { if(i<=n) { if(a[i]==key) return 1; else return linsearch(a,i+1,n,key); } else return -1; } void main() { int a[20],n,key,i,pos,choice; clock_t start,end; float duration; for(;;) { clrscr(); printf("1. Binary Search\n 2. Linear Search\n 3. Exit\n"); printf("\n Enter Your Choice :"); scanf("%d", &choice); if(choice >= 3) exit(0); else { printf("\n Enter the number of elements : "); scanf("%d",&n);
Algorithms Laboratory
Manual
printf("\n Enter the elements \n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n Enter the key to be searched : "); scanf("%d",&key); switch(choice) { case 1: start=clock(); pos=binsearch(a,key,0,n-1); end=clock(); duration=(end-start)/CLK_TCK; printf("\n Time taken is %f\n",duration); if(pos==-1) printf("\n key is not found\n"); else printf("\n key is found at %d position\n",pos); break; case 2: start=clock(); pos=linsearch(a,0,n,key); delay(100); end=clock(); duration=(end-start)/CLK_TCK; printf("\n Time taken is %f",duration); if(pos==-1) printf("\n key not found\n"); else printf("\n Key is found at %d position\n",pos); break; } getch(); } } }
Algorithms Laboratory
Manual
Output: 1. Binary search 2. Linear search 3. Exit Enter your choice: 1 Enter the number of elements: 5 Enter the elements 11 22 33 44 55 Enter the key to be searched: 33 Time taken is 0.109890 Key is found 1. Binary search 2. Linear search 3. Exit Enter your choice: 2 Enter the number of elements: 4 Enter the elements 1 6 3 8 Enter the key to be searched: 8 Time taken is 0.109890 Key is found 1. Binary search 2. Linear search 3. Exit Enter your choice: 1 Enter the number of elements: 4 Enter the elements 23 45 67 78 Enter the key to be searched: 11 Time taken is 0.109890 Key is not found 1. Binary search 2. Linear search 3. Exit Enter your choice: 3
Algorithms Laboratory
Manual
2.
Sort a given set of elements using the Heapsort method and determine the time required to sort the elements. Repeat the experiment for different values of n, the number of elements in the list to be sorted and plot a graph of the time taken versus n. #include<stdio.h> #include<conio.h> void heapcon(int a[],int n) { int i,j,k,item; for(i=2;i<=n;i++) { item=a[i]; j=i; k=j/2; while(k!=0 && item > a[k]) { a[j]=a[k]; j=k; k=j/2; } a[j]=item; } } void adjust(int a[],int n) { int item,i,j; j=1; item=a[j]; i=2*j; while(i<n) { if((i+1)<n) { if(a[i]<a[i+1]) i++; } if(item<a[i]) { a[j]=a[i]; j=i; i=2*j; } else break; } a[j]=item; } void heapsort(int a[],int n) { int i,temp; heapcon(a,n);
Algorithms Laboratory
Manual
for(i=n;i>=1;i--) { temp=a[1]; a[1]=a[i]; a[i]=temp; adjust(a,i); } } void main() { int i,n,a[20]; clrscr(); printf("\n Enter the number of elements : "); scanf("%d",&n); printf("\n Enter the elements \n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); heapsort(a,n); printf("\n The sorted array is \n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); getch(); }
3.
Sort a given set of elements using Merge sort method and determine the time required to sort the elements. Repeat the experiment for different values of n, the number of elements in the list to be sorted and plot a graph of the time taken versus n. #include<stdio.h> #include<conio.h> void merge(int a[],int low,int mid,int high) { int i,j,k,b[15]; i=low; j=mid+1; k=low; while((i<=mid) && (j<=high)) { if(a[i]<a[j]) { b[k]=a[i]; k=k+1; i=i+1; } else { b[k]=a[j]; k=k+1; j=j+1; }
Algorithms Laboratory
Manual
} while(i<=mid) { b[k]=a[i]; k=k+1; i=i+1; } while(j<=high) { b[k]=a[j]; k=k+1; j=j+1; } for(i=low;i<=k-1;i++) a[i]=b[i]; } void mergesort(int a[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,mid,high); } } void main() { int i,n,a[15]; clrscr(); printf("\n Enter the number of elements : "); scanf("%d",&n); printf("\n Enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(a,0,n-1); printf("\n The sorted elements are\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); }
Algorithms Laboratory
Manual
4.
Sort a given set of elements using Selection sort and determine the time required to sort elements. Repeat the experiment for different values of n, the number of elements in the list to be sorted and plot a graph of the time taken versus n. #include<stdio.h> #include<conio.h> #include<time.h> int min(int a[],int k,int n) { int loc,j,min; min=a[k]; loc=k; for(j=k+1;j<=n-1;j++) { if(min > a[j]) { min=a[j]; loc=j; } } return loc; } void main() { int i,a[20],n,k,pos=0,temp; clock_t start,end; float duration; clrscr(); printf("\n Enter the number of elemnts : "); scanf("%d",&n); printf("\n Enter the elements of the array :\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); start=clock(); for(k=0;k<n;k++) { pos=min(a,k,n); temp=a[k]; a[k]=a[pos]; a[pos]=temp; } delay(100); end=clock(); duration=(end-start)/CLK_TCK; printf("\n The sorted array is \n"); for(i=0;i<n;i++) printf("%d\n",a[i]); printf("\n \n The time required to sort is %f\n",duration); getch(); }
Algorithms Laboratory
Manual
7.
Sort a given set of elements using Quick sort method and determine the time required sort the elements. Repeat the experiment for different values of n, the number of elements in the list to be sorted and plot a graph of the time taken versus n. #include<stdio.h> #include<conio.h> int partition(int a[],int low,int high) { int i,j,temp,key; key=a[low]; i=low+1; j=high; while(1) { while(i<high && key >=a[i]) i++; while(key<a[j]) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } else { temp=a[low]; a[low]=a[j]; a[j]=temp; return j; } } } void quicksort(int a[],int low,int high) { int j; if(low<high) { j=partition(a,low,high); quicksort(a,low,j-1); quicksort(a,j+1,high); } } void main() { int i,n,a[20]; clrscr(); printf("\n Enter the number of elements : "); scanf("%d",&n); printf("\n Enter the elements \n"); for(i=0;i<n;i++)
Algorithms Laboratory
Manual
scanf("%d",&a[i]); quicksort(a,0,n-1); printf("\n The sorted elements are \n"); for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); } 13. a. Implement Floyds algorithm for the All-Pairs- Shortest-Paths problem. */ #include<stdio.h> #include<conio.h> #define INFINITY 999 int min(int a,int b) { return a<b?a:b; } void floyd(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]=min(p[i][j], p[i][k]+p[k][j]); } void main() { int a[10][10],n,i,j; clrscr(); printf("\n Enter the number of vertices: "); scanf("%d",&n); printf("\n Enter the cost matrix 0 -self loop & 999 for no edge\n"); for (i=1;i<=n;i++) for (j=1;j<=n;j++) scanf("%d",&a[i][j]); floyd(a,n); printf("\n Shortest path matrix\n"); for (i=1;i<=n;i++) { for (j=1;j<=n;j++) printf("%d\t",a[i][j]); printf("\n"); } getch(); }