of 26 /26
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;

# Daa practicals

Tags:

Embed Size (px)

DESCRIPTION

All

### Text of Daa practicals

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;

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;

}

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);

}

}

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++)

printf("%d ", arr[i]);

printf("\n");

quicksort(arr,0,n-1);

printf("Sorted list is :\n");

for (i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

getch();

}

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("\n\n 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();

}

/* 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++];

else

x[j+end]=executing[mrg2++];

else

x[j+end]=executing[mrg1++];

}

}

4. Wap to implement Radix sort algorithm.

# define NUMLETS 100

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

int n,a[20],i;

clrscr();

printf("enter no:");

scanf("%d",&n);

printf("enter the data");

for(i=0;i<=n;n++)

{

printf("%d",i+1);

scanf("%d",&a[i]);

}

getch();

}

{

int rear[10],front[10],first,p,q,exp,k,i,y,j;

struct

{

int info;

int next;

}

node[NUMLETS];

for(i=0;i<n-1;i++)

{

node[i].info=a[i];

node[i].next=i+1;

}

node[n-1].info=a[n-1];

node[n-1].next=-1;

first=0;

for(k=1;k<=2;k++)

{

for(i=0;i<10;i++)

{

front[i]=-1;

rear[i]=-1;

}

while(first!=-1)

{

p=first;

first=node[first].next;

y=node[p].info;

exp=pow(10,k-1);

j=(y/exp)%10;

q=rear[j];

if(q==-1)

front[j]=p;

else

node[q].next=p;

rear[j]=p;

}

for(j=0;j<10&&front[j]==-1;j++)

if(i<=9)

{

p=i;

node[rear[j]].next=front[i];

}

j=i;

}

node[rear[p]].next=-1;

}

//CPOY INTO orignal array

for(i=0;i<n;i++)

{

a[i]=node[first].info;

first=node[first].next;

}

clrscr();

textcolor(yellow);

cprintf("data after sorting");

for(i=0;i<n;i++)

printf("%d%d,i+1,a[i]);

}

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 vertices\n");

scanf("%d",&num_of_vertices);

if(num_of_vertices<=0)

{

printf("\n this is meaningless \n);

exit(1);

}

for(i=1;i<=num_0f_vertices;i++)

printf("\n enter elements of row%d\n",i);

for(j=1;j<=num_of_vertices;j++)

{

scanf("%d",&graph[i][j]);

}

}

printf("\n enter source vertex \n");

scanf("%d",&source);

for(j=1;j<num_of_vertices;j++)

{

mark[j]=0;

pathestimate=0;

predecessor[j]=0;

}

pathestimate=0;

while(count<num_of_vertices)

{

u=minimum(pathestimate,mark,num_of_vertices);

s[++count]=u;

mark[u]=1;

for(i=1;i<=num_of_vertices;i++)

{

if(graph[u][i]>0)

{

if(mark[i]!=1)

{

if(pathestimate[i]>pathestimate[u]+graph[u][i])

{

pathestimate[i]=pathestimate[u]+graph[u][i];

predecessor[i]=u;

}

}

}

}

}

for(i=1;i<=num_of_vertices;i++)

{

printpath(souce,i,predecessor);

if(pathestimate[i]!=999)

printf("->(%d)\n",pathestimate[i]);

}

}

int minimum(int a[],int m[],int k)

{

int mi=999;

int i,t;

for(1=1;i<=k;i++)

{

if(m[i]!=1)

{

if(mi>a[i])

{

mi=a[i];

t=i;

}

}

}

return t;

}

void printpath(int x,int i,int[p])

{

printf("\n");

if(i==x)

{

printf("%d",x);

}

else if(p[i]==0)

printf("no path from %d to %d",x,i);

else

{

printpath(x,p[i],p);

printf("%d",i);

}

}

6. Wap to implement Warshall algorithm .

#include<conio.h>

#define infinity 9999

#define MAX 20

main()

{

int i,j,k,n;

printf("enter number of vertices:");

scanf("%d",&n);

printf("enter weighted matrix:\n");

for(i=0;i<N;i++)

for(j=0;j<N;j++)

printf("weighted matrix is:\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

path[i][j]=infinity;

for(k=0;k<n;k++)

{

printf("q%dis:\n",k);

display(path,n);

for(i=0;i<n;i++)

for(j=0;j<n;j++)

path[i][j]=mminimum(path[i][j],path[i][j]+path[k][j]);

}

printf("shortest path matrix q%d is:\n",k);

display(path,n);

}

minimum(int a,intb)

{

if(a<=b)

return a;

else b;

}

dieplay(int matrix[max][max],int n);

{

int i,j;

for(i=o;i<n;i++)

{

for(j=0;j<n;j++)

printf("%d",matrix[i][j]);

printf("\n");

}

}

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--)

arr[i+1]=arr[i];

arr[i+1]=k;

printf("Pass %d, Element inserted in proper place: %d\n",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");

}

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;

}

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;

}

/*reducing the value equal to index of an array*/

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*/

counter[j].value=ar[i];

else

{

/*if there is only one element at that index*/

{

n2->value=ar[i];

continue;

}

/*if there is already an node in the list at the index*/

{

}

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*/

continue;

else

{

n1=&counter[i];

n2=&counter[i};

/*if there is more than one node at this index*/

{

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++)

{

}

}

/*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();

}

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 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];

for(i=0;i<w;I++)

printf("weight %d;benifit:%d;to reach this weight i added %d (%ds %dkg)to

else

printf("---\n");

aux=w;

{

}

}

int main(int argc,char*arvg[])

{

fill_sack();

return 0;

}

Documents
Documents
Documents
Documents
Documents
Documents
Documents
Engineering
Documents
Documents
Engineering
Education
Education
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Documents
Education
Documents
Documents
Documents
Education