SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
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("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();
}
/* 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 radixsort(int a[],int);
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]);
}
radixsort(a,n);
getch();
}
void radixsort(int a[],int n)
{
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 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]);
}
}
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;
int adj[max][max],path[max][max];
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++)
scanf("%d",&adj[i][j]);
printf("weighted matrix is:n");
display(adj,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(adj[i][j]==0)
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: %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");
}
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*/
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;
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++)
{
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();
}
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
weight %d.n",a[i],last_added[i]+1,v[last_added[i]],c[last_added[i],i-c[last_added[i]]);
else
printf("---n");
aux=w;
while((aux>0)&&(last_added[aux]!=-1))
{
printf("added object %d(ds %dkg).space left:%d n,last_added[aux]+1,v[last_added[aux]],
c[last_added[aux]],aux-c[last_added[aux]]);
aux-=c[last_added[aux]];
}
printf("total value added :%dsn",a[w]);
}
int main(int argc,char*arvg[])
{
fill_sack();
return 0;
}

Más contenido relacionado

La actualidad más candente

Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programsKandarp Tiwari
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using CBilal Mirza
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming LanguageArkadeep Dey
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresLakshmi Sarvani Videla
 
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating SystemLPU
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignmentsreekanth3dce
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)Make Mannan
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C LanguageRAJWANT KAUR
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanriturajj
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shortingargusacademy
 

La actualidad más candente (20)

Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Pnno
PnnoPnno
Pnno
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
Ada file
Ada fileAda file
Ada file
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Programs for Operating System
Programs for Operating SystemPrograms for Operating System
Programs for Operating System
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
 
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)Lab manual operating system [cs 502 rgpv] (usefulsearch.org)  (useful search)
Lab manual operating system [cs 502 rgpv] (usefulsearch.org) (useful search)
 
C programs
C programsC programs
C programs
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
C programms
C programmsC programms
C programms
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 

Similar a Daapracticals 111105084852-phpapp02

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointersvinay arora
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdfsowmya koneru
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkarsandeep kumbhkar
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
C basics
C basicsC basics
C basicsMSc CST
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2Koshy Geoji
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207Syed Tanveer
 
Write a program in Scheme using quicksort algorithm and test it for an.docx
Write a program in Scheme using quicksort algorithm and test it for an.docxWrite a program in Scheme using quicksort algorithm and test it for an.docx
Write a program in Scheme using quicksort algorithm and test it for an.docxlez31palka
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or notaluavi
 
Write a function in LUA that finds the real valued average of its argu.docx
Write a function in LUA that finds the real valued average of its argu.docxWrite a function in LUA that finds the real valued average of its argu.docx
Write a function in LUA that finds the real valued average of its argu.docxlez31palka
 

Similar a Daapracticals 111105084852-phpapp02 (20)

Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C programs
C programsC programs
C programs
 
Ds
DsDs
Ds
 
array.ppt
array.pptarray.ppt
array.ppt
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Lab Question
Lab QuestionLab Question
Lab Question
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Dvst
DvstDvst
Dvst
 
C basics
C basicsC basics
C basics
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
Write a program in Scheme using quicksort algorithm and test it for an.docx
Write a program in Scheme using quicksort algorithm and test it for an.docxWrite a program in Scheme using quicksort algorithm and test it for an.docx
Write a program in Scheme using quicksort algorithm and test it for an.docx
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 
Write a function in LUA that finds the real valued average of its argu.docx
Write a function in LUA that finds the real valued average of its argu.docxWrite a function in LUA that finds the real valued average of its argu.docx
Write a function in LUA that finds the real valued average of its argu.docx
 
C programs
C programsC programs
C programs
 

Daapracticals 111105084852-phpapp02

  • 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++)
  • 5. 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(); }
  • 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++];
  • 9. 4. Wap to implement Radix sort algorithm. # define NUMLETS 100 #include<stdio.h> #include<conio.h> #include<math.h> void radixsort(int a[],int); 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]); } radixsort(a,n); getch(); } void radixsort(int a[],int n) { int rear[10],front[10],first,p,q,exp,k,i,y,j;
  • 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]); } }
  • 14. 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; } }
  • 15. } } } 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;
  • 16. } 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); } }
  • 17. 6. Wap to implement Warshall algorithm. #include<conio.h> #define infinity 9999 #define MAX 20 main() { int i,j,k,n; int adj[max][max],path[max][max]; 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++) scanf("%d",&adj[i][j]); printf("weighted matrix is:n"); display(adj,n); for(i=0;i<n;i++) for(j=0;j<n;j++) if(adj[i][j]==0) path[i][j]=infinity; for(k=0;k<n;k++) { printf("q%dis:n",k); display(path,n);
  • 18. 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"); } }
  • 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
  • 26. weight %d.n",a[i],last_added[i]+1,v[last_added[i]],c[last_added[i],i-c[last_added[i]]); else printf("---n"); aux=w; while((aux>0)&&(last_added[aux]!=-1)) { printf("added object %d(ds %dkg).space left:%d n,last_added[aux]+1,v[last_added[aux]], c[last_added[aux]],aux-c[last_added[aux]]); aux-=c[last_added[aux]]; } printf("total value added :%dsn",a[w]); } int main(int argc,char*arvg[]) { fill_sack(); return 0; }