1. 1. Wap to implement Heap sort algorithm.
#include<stdio.h>
#include<conio.h>
void restoreHup(int*,int);
void restoreHdown(int*,int,int);
void main()
{
int a[20],n,i,j,k;
printf("enter the number of element to sort:");
scanf("%d",&n);
printf("enter the element:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
restoreHup(a,i);
}
j=n;
for(i=1;i<=j;i++)
{
int temp;
temp=a[1];
a[1]=a[n];
a[n]=temp;
n--;
restoreHdown(a,1,n);
}
n=j;
2. printf("here is it.....");
for(i=1;i<=n;i++)
{
printf("%4d",a[a]);
}
void restoreHup(int*a,int i)
{
int v=a[i];
while((i>1)&&(a[i/2])<v))
{
a[i]=a[i/2];
i=[i/2];
}
a[i]=v;
}
void restoreHdown(int*a,int i,int n)
{
int v=a[i];
int j=i*2;
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
j++;
if(a[j]<a[j/2])break;
a[j/2]=a[j];
j=j*2;
}
a[j/2]=v;
}
3. 2. Write a program to implement Quick sort algorithm.
/* Program of sorting using quick sort */
#include <stdio.h>
#include<conio.h>
#define MAX 20
exchange(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
int partition(int a[],int p, int r)
{
int x=a[r];
int i=p-1;
int j;
for(j=p;j<=r-1;j++)
{
if(a[j]<=x)
{
i=i+1;
exchange(a,i,j);
}
4. }
exchange(a,i+1,r);
return i+1;
}
quicksort(int a[], int p, int r)
{
int q;
if(p<r)
{
q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
main()
{
int arr[MAX],i,j,k,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is :n");
for (i = 0; i < n; i++)
6. 3. Write a program to implement Merge sort algorithm.
#include<stdio.h>
#include<conio.h>
#define Max_ARY 10
void merge_sort(int x[],int end,int start);
int main(void)
{
int arry[MAX_ARY];
int j=0;
printf("nn enter the elements to be sorted:n");
for(j=0;j<MAX_ARY;j++)
scanf("%d",&ary[j]);
/*arry before mergesort*/
printf("before");
for(j=0;j<MAX_ARY;j++)
printf("%d",ary[j]);
printf("n");
merge_sort(ary,0,MAX_ARY-1);
/*array after mergesort*/
printf("after merge sort:");
for(j=0;j<MAX_ARY;j++)
printf("%d",ary[j]);
printf("n");
getch();
}
7. /* Method to implement merge sort*/
void merge_sort(int x[],int end,int start)
{
int j=0;
const int size=start-end+1;
int mid=0;
int mrg1=0;
int mrg2=0;
int executing[MAX-ARY];
if(end==start)
return;
mid=(ent+start)/2;
merge_sort(x,end,mid);
merge_sort(x,mid+1,start);
for(j=0;j<size;j++)
executeing[j]=x[end+j];
mrg1=0;
mrg2=mid-end+1;
for(j=0;j<size;j++)
{
if(mrge2<=start-end)
if(mrg1<=mid-end)
if(executing[mrg1]>executing[mrg2])
x[j+end]=executing[mrg2++];
else
x[j+end]=executing[mrg1++];
13. 5. Wap to implement Dijkstra’s algorithm.
#include<stdio.h>
#include<conio.h>
void main()
int graph[15][15],s[15],pathestimate[15],mark[15];
int num_of_vertices,source,i,j,u,predecessor[15];
int count=0;
int minimum(int a[],int m[],int k);
void printpath(int,int,int[]);
printf("n enter no of verticesn");
scanf("%d",&num_of_vertices);
if(num_of_vertices<=0)
{
printf("n this is meaningless n);
exit(1);
}
printf("n enter adjacent matrix n");
for(i=1;i<=num_0f_vertices;i++)
printf("n enter elements of row%dn",i);
for(j=1;j<=num_of_vertices;j++)
{
scanf("%d",&graph[i][j]);
}
}
19. 7. Write a program to implement Insertion sort algorithm.
#include <stdio.h>
#include<conio.h>
#define MAX 20
void main()
{
int arr[MAX],i,j,k,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is :n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
/*Insertion sort*/
for(j=1;j<n;j++)
{
k=arr[j]; /*k is to be inserted at proper place*/
for(i=j-1;i>=0 && k<arr[i];i--)
20. arr[i+1]=arr[i];
arr[i+1]=k;
printf("Pass %d, Element inserted in proper place: %dn",j,k);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
}
printf("Sorted list is :n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
}
21. 8. Wap to implement Bucket Sort algorithm.
#include<stdio.h>
#include<conio.h>
/*Defining the structure for each node in the list*/
typedef struct node
{
float value;
struct node*link;
}
node;
void main()
{
/*An array of structure,pointers to the structure*/
node counter[10],*n2,*n1;
float ar[10]={0.79,0.13,0.16,0.64,0.39,0.20,0.89,0.53,0.71,0.42};
float fa[10],temp;
int i,j,k=0;
float n,a;
clrscr();
/*initialising the elements of the counter array to zero*/
for(i=0;i<10;i++)
{
counter[i].value=0;
counter[i].link=0;
}
/*reducing the value equal to index of an array*/
22. for(i=0;i<10;i++)
{
n=ar[i];
j=n*100;
j=j/10;
/*moving the values in to the apropriate bucket*/
/*if there are no elements in the bucket*/
if(counter[j].value==0 && counter[j].link==0)
counter[j].value=ar[i];
else
{
/*if there is only one element at that index*/
if(counter[j].link==0 && counter[j].value !=0)
{
counter[j].link=(node*) malloc(sizeof(node));
n2=counter[j].link;
n2->link=0;
n2->value=ar[i];
continue;
}
/*if there is already an node in the list at the index*/
n2=counter[j].link;
while(n2->link !=0)
{
n2=n2 -> link;
}
n2 ->link=(node *) malloc(sizeof(node));
n2= n2 -> link;
n2 -> link=0;
23. n2 -> value=ar[i];
}
}
/*sorting of all the buckets in order*/
printf(*the sorted values after merging all bucket in order are:");
for(i=0;i<10;i++)
{
/*No nodes at that index*/
if(counter[1].link==0 && counter[i].value==0)
continue;
else
{
n1=&counter[i];
n2=&counter[i};
/*if there is more than one node at this index*/
if(n2-> link !=0)
{
while(n1 !=0)
{
while(n2 !=0)
{
if(n1 -> value > n2 ->value)
{
temp=n1 -> value;
n1 ->value= n2 ->value;
n2 -> value=temp;
}
n1=& counter[i];
for(; n1!=0; k++)
24. {
fa[k]=n1 -> link;
}
}
/*if there is only one node at this index*/
else{
fa[k]=counter[i].value;
k=k+1;
}
}
}
for(i=0;i<10;i++)
printf("%f",fa[i]);
getch();
}
25. 9. Write a program to implement Dynamic programming algorithm for
knapsack problem
#include<stdio.h>
#include<conio.h>
#define MAXWEIGHT 100
int n=3;
int c[10]={8,6,4}
int v[10]={16,10,7}
int w=10;
void fill_sack()
int a[maxweight];
int last_added[MAXWEIGHT];
int i,j;
int aux;
for(i=1;i<=w;i++)
for(j=0;j<=n;j++)
if((c[j]<=i)&& (a[i]<a[i-c[j]]+v[j]))
{
a[i]=a[i-c[j]]+v[j];
last_added[i]=j;
for(i=0;i<w;I++)
if(last_added[i]!=-1)
printf("weight %d;benifit:%d;to reach this weight i added %d (%ds %dkg)to