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

Daa Lab Manual (Part A)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Daa Lab Manual (Part A)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

PART - A

1. Write a program to sort a list of N elements using Selection Sort.


SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, a[10],i,j,temp,min;
clrscr();
printf("Enter the size of the array");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("Sorted Elements:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
OUTPUT:
2. Write a Program to find the maximum and minimum value in array using Divide and Conquer.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int max, min;
int a[100];
void maxmin(int low, int high)
{
int max1, min1, mid;
if(low==high)
{
max = min = a[low];
}
else
{
if(low == high-1)
{
if(a[low] <a[high])
{
max = a[high];
min = a[low];
}
else
{
max = a[low];
min = a[high];
}
}
else
{
mid = (low+high)/2;
maxmin(low, mid);
max1 = max;
min1 = min;
maxmin(mid+1, high);
if(max1>max)
max = max1;
if(min1 < min)
min = min1;
}
}
}
void main ()
{
int i, num;
clrscr();
printf ("\nEnter the total number of numbers : ");
scanf ("%d",&num);
printf ("Enter the numbers : \n");
for (i=0;i<num;i++)
scanf ("%d",&a[i]);
max = a[0];
min = a[0];
maxmin(0, num-1);
printf ("Minimum element in an array : %d\n", min);
printf ("Maximum element in an array : %d\n", max);
getch();
}
OUTPUT:
3. Write a program to implement merge sort algorithm for sorting a list of integers in ascending
order.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void merge(int a[10],int low,int mid, int high);
void mergesort(int a[10],int low,int high);
void main()
{
int i,n,a[10];
clrscr();
printf("Enter the size of the array");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("Sorted elements are:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void mergesort(int a[10],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 merge(int a[10],int low,int mid,int high)
{
int i,j,k,b[10],r;
i=low;
k=low;
j=mid+1;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
{
b[k]=a[i];
i++;
k++;
}
else
{
b[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
b[k]=a[i];
k++;
i++;
}
while(j<=high)
{
b[k]=a[j];
k++;
j++;
}
for(i=low;i<=high;i++)
a[i]=b[i];
}
OUTPUT:
4. Write a program to sort list of integer in ascending order using Quick Sort Algorithm.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void quicksort(int a[10], int low,int high)
{
int j;
if(low < high)
{
j=partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}
int partition(int a[10],int low,int high)
{
int pivot,j,temp,i;
pivot=low;
i=low;
j=high;
while(i<j)
{
while(a[i]<=a[pivot])
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
return j;
}
void main()
{
int n,a[50],i;
clrscr();
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("Sorted Array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
OUTPUT:
5. Sort a given set of n integer elements using Merge Sort method and compute its time
complexity. Run the program for varied values of n> 5000, and record the time taken to
sort.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
void merge(int a[50],int low,int mid, int high);
void mergesort(int a[50],int low,int high);
double tc,t;
clock_t start,end;
void main()
{
int i,n,a[50];
clrscr();
printf("Enter the size of the array");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
start=clock();
printf("Starting Clock=%ld\n",start);
mergesort(a,0,n-1);
end=clock();
printf("End Clock=%ld\n",end);
printf("Sorted elements are:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
t=n*log(n);
printf(“Time Complexity=%f”,t);
tc=(double)(end-start)/CLOCKS_PER_SEC;
printf("Time Efficiency=%f",tc);
getch();
}
void mergesort(int a[50],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 merge(int a[50],int low,int mid,int high)
{
int i,j,k,b[50],r;
i=low;
k=low;
j=mid+1;
while((i<=mid)&&(j<=high))
{
if(a[i]<a[j])
{
b[k]=a[i];
i++;
k++;
}
else
{
b[k]=a[j];
j++;
k++;
}
}
while(i<=mid)
{
b[k]=a[i];
k++;
i++;
}
while(j<=high)
{
b[k]=a[j];
k++;
j++;
}
for(i=low;i<=high;i++)
a[i]=b[i];
}
OUTPUT:
6. Sort a given set of n integer elements using Quick Sort method and compute its time complexity.
Run the program for varied values of n> 5000 and record the time taken to sort.
SOURCE CODE:

#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<math.h>
clock_t start,end;
double tc,t;
void quicksort(int a[10], int low,int high)
{
int j;
if(low < high)
{
j=partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}
int partition(int a[10],int low,int high)
{
int pivot,j,temp,i;
pivot=low;
i=low;
j=high;
while(i<j)
{
while(a[i]<=a[pivot])
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
return j;
}
void main()
{
int n,a[50],i;
clrscr();
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
start=clock();
printf("Starting Clock=%ld\n",start);
quicksort(a,0,n-1);
end=clock();
printf("Ending Clock=%ld\n",end);
printf("Sorted Array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
t=n*log(n);
printf(“Time Complexity=%f”,t)
tc=(double)(start-end)/CLOCKS_PER_SEC;
printf("Time Duration=%f",tc);
getch();
}
OUTPUT:
7. Write a program to implement 0/1 knapsack problem using dynamic programming.
SOURCE CODE:

#include<stdio.h>
#include<conio.h>
int i,j,n,capacity,w[50],p[50],maxprofit;
int maximum(int x,int y)
{
if(x>y)
return x;
else
return y;
}
knapsack(int i,int c)
{
if(i==n)
return((c<w[n])?0:p[n]);
if(c<w[i])
return knapsack(i+1,c);
return maximum(knapsack(i+1,c),knapsack(i+1,c-w[i])+p[i]);
}
void main()
{
clrscr();
printf("Enter the no.of objects");
scanf("%d",&n);
printf("Enter the weights");
for(i=0;i<n;i++)
scanf("%d",&w[i]);
printf("Enter the profits");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter the capacity");
scanf("%d",&capacity);
maxprofit=knapsack(0,capacity);
printf("Maximum Profit=%d",maxprofit);
getch();
}
OUTPUT:
8. Write a program to implement knapsack algorithm using Greedy Solution.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void knapsackGreedy(int capacity,int n,int w[20],int P[20])
{
double x[20];
double sum=0.0;
int i,j;
double ratio[20];
int order[20];
int Rc=capacity;
for(i=0;i<n;i++)
{
ratio[i]=(double)P[i]/w[i];
}
for(i=0;i<n;i++)
{
order[i]=i;
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(ratio[order[j]]<ratio[order[j+1]])
{
int tempIndex=order[j];
order[j]=order[j+1];
order[j+1]=tempIndex;
}
}
}
for(i=0;i<n;i++)
{
x[i]=0;
}
for(i=0;i<n;i++)
{
int currentIndex=order[i];
if(w[currentIndex]<=Rc)
{
x[currentIndex]=1;
Rc=Rc-w[currentIndex];
sum=sum+P[currentIndex];
}
else
{
x[currentIndex]=(double)Rc/w[currentIndex];
sum+=(P[currentIndex]*x[currentIndex]);
break;
}
}
printf("\nMaximum profit: %.21f\n",sum);
}
int main()
{
int capacity,n,i,w[20],P[20];
clrscr();
printf("Enter the number of objects(n): ");
scanf("%d",&n);
printf("Enter Knapsack capacity(M): ");
scanf("%d",&capacity);

printf("Enter weights for each object:\n");


for(i=0;i<n;i++)
{
scanf("%d",&w[i]);
}
printf("Enter profits for each object:\n");
for(i=0;i<n;i++)
{
scanf("%d",&P[i]);
}
knapsackGreedy(capacity, n, w, P);
getch();
return 0;
}
9. Write a program to perform Travelling Salesman Problem.
SOURCE CODE:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[10][10],visited[10],n,cost=0;
void get()
{
int i,j;
printf("\n\nEnter Number of Cities: ");
scanf("%d",&n);
printf("\nEnter Cost Matrix: \n");
for(i=0;i<n;i++)
{
printf("\n Enter Elements of Row #:%d\n",i+1);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
visited[i]=0;
}
printf("\n\nThe Cost Matrix is:\n");
for(i=0;i<n;i++)
{
printf("\n\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
}
void mincost(int city)
{
int i,ncity,least(int city);
visited[city]=1;
printf("%d ===>",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=a[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i<n;i++)
{
if((a[c][i]!=0)&&(visited[i]==0))
if(a[c][i]<min)
{
min=a[i][0]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
void put()
{
printf("\n\nMinimum cost:");
printf("%d",cost);
}
void main()
{
clrscr();
get();
printf("\n\nThe Path is:\n\n");
mincost(0);
put();
getch();
}

OUTPUT:

You might also like