Escolar Documentos
Profissional Documentos
Cultura Documentos
OF ALGORITHM
PRACTICAL FILE
#include<stdio.h>
#include<conio.h>
int GCD (int a,int b)
{
if (a<0) a= -a;
if (b<0) b= -b;
if (a==0 || b==1 || a==b) return b;
if (a==1 || b==0) return a;
if (a>b) return GCD (b, a%b);
else return GCD (a, b%a);
}
void main()
{
int x,y;
clrscr();
printf("Enter 1st number:");
scanf("%d",&x);
printf("Enter 2nd number:");
scanf("%d",&y);
OUTPUT
Enter two numbers for GCD
1. 35
2. 75
The GCD is: 5
PROGRAM 2
WAP TO IMPLEMENT BINARY SEARCH ALGORITHM
RECURSIVELY
#include<stdio.h>
int binarysearch(int a[],int n,int low,int high)
{ int mid;
if (low > high)
return -1;
mid = (low + high)/2;
if(n == a[mid])
{ printf("The element is at position %d\n",mid+1);
return 0;
}
if(n < a[mid])
{ high = mid - 1;
binarysearch(a,n,low,high);
}
if(n > a[mid])
{ low = mid + 1;
binarysearch(a,n,low,high);
}
}
int main()
{ int a[50];
int n,no,x,result;
printf("Enter the number of terms : ");
scanf("%d",&no);
printf("Enter the elements :\n");
for(x=0;x<no;x++)
scanf("%d",&a[x]);
printf("Enter the number to be searched : ");
scanf("%d",&n);
result = binarysearch(a,n,0,no-1);
if(result == -1)
printf("Element not found");
return 0;
}
OUTPUT
OUTPUT
Enter the number of Disks : 3
#include <stdio.h>
#include <stdlib.h>
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int choose_pivot(int i,int j )
{
return((i+j) /2);
}
void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if( m < n)
{
k = choose_pivot(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap(&list[i],&list[j]);
}
// swap two elements
swap(&list[m],&list[j]);
// recursively sort the lesser list
quicksort(list,m,j-1);
quicksort(list,j+1,n);
}
}
void printlist(int list[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}
void main()
{
int MAX_ELEMENTS;
printf("How many numbers you want to sort:");
scanf("%d",&MAX_ELEMENTS);
int list[50];
int i = 0;
// generate random numbers and fill them to the list
printf("Enter the numbers\n");
for(i = 0; i < MAX_ELEMENTS; i++ ){
scanf("%d",&list[i]);
}
printf("The list before sorting is:\n");
printlist(list,MAX_ELEMENTS);
// sort the list using quicksort
quicksort(list,0,MAX_ELEMENTS-1);
OUTPUT
How many numbers you want to sort: 5
Enter the numbers:
66
11
44
33
22
The list before sorting:
66 11 44 33 22
The list after sorting:
11 22 33 44 66
PROGRAM 5
WAP TO IMPLEMENT THE MERGE SORT ALGORITHM
#include<stdio.h>
#include<conio.h>
int j,a[15],i,temp,n;
void main()
{
clrscr();
void createheap(int);
printf("Enter number of elements(Not more than 15)\n ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=n;i>=2;i--)
createheap(i);
for(i=n-1;i>=1;i--)
{
temp=a[1];
a[1]=a[i+1];
a[i+1]=temp;
for(j=i;j>=2;j--)
createheap(j);
}
printf("The sorted elements are :\n");
for(i=1;i<=n;i++)
{
printf("%d ",a[i]);
}
getch();
}
void createheap(int y)
{
if(y>1)
{
if(a[y]>a[y/2])
{
temp=a[y];
a[y]=a[y/2];
a[y/2]=temp;
createheap(y/2);
}}}
OUTPUT
#include<iostream>
using namespace std;
class prims
{
private:
int n;
int graph_edge[250][4];
int g;
int tree_edge[250][4];
int t;
int s;
int T1[50],t1;
int T2[50],t2;
public:
void input();
int findset(int);
void algorithm();
void output();
};
void prims::input()
{
cout<<"Enter the no. of nodes in the undirected weighted graph ::";
cin>>n;
g=0;
cout<<"Enter the weights for the following edges ::\n";
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
cout<<" < "<<i<<" , "<<j<<" > ::";
int w;
cin>>w;
if(w!=0)
{
g++;
graph_edge[g][1]=i;
graph_edge[g][2]=j;
graph_edge[g][3]=w;
}
}
}
// print the graph edges
cout<<"\n\nThe edges in the given graph are::\n";
for(int i=1;i<=g;i++)
cout<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" >
::"<<graph_edge[i][3]<<endl;
}
int prims::findset(int x)
{
for(int i=1;i<=t1;i++)
if(x==T1[i])
return 1;
for(int i=1;i<=t2;i++)
if(x==T2[i])
return 2;
return -1;
}
void prims::algorithm()
{
t=0;
t1=1;
T1[1]=1; //The source node
t2=n-1;
for(int i=1;i<=n-1;i++)
T2[i]=i+1; //The reamining nodes
cout<<"\n*****The algorithm starts*****\n\n";
while(g!=0 && t!=n-1)
{
// Find the least cost edge
int min=9999;
int p;
int u,v,w;
for(int i=1;i<=g;i++)
{
bool flag1=false,flag2=false;
//if u and v are in different sets
if(findset(graph_edge[i][1])!=findset(graph_edge[i][2]))
{
if(min>graph_edge[i][3])
{
min=graph_edge[i][3];
u=graph_edge[i][1];
v=graph_edge[i][2];
w=graph_edge[i][3];
p=i;
}
}
}
//break if there is no such edge
cout<<"The edge included in the tree is ::";
cout<<" < "<<u<<" , "<<v<<" > "<<endl;
int x;
for(x=1;T2[x]!=m;x++);
for(;x<t2;x++)
T2[x]=T2[x+1];
t2--;
// Print the sets
int k;
cout<<"NOW\nT1 :: ";
for(k=1;k<=t1;k++)
cout<<T1[k]<<" ";
cout<<endl;
cout<<"T2 :: ";
for(k=1;k<=t2;k++)
cout<<T2[k]<<" ";
cout<<endl;
cout<<"The graph edges are ::\n";
for(int i=1;i<=g;i++)
cout<<" < "<<graph_edge[i][1]<<" , "<<graph_edge[i][2]<<" >
::"<<graph_edge[i][3]<<endl;
cout<<endl<<endl;
}
}
void prims::output()
{
cout<<"\nThe selected edges are ::\n";
for(int i=1;i<=t;i++)
cout<<" < "<<tree_edge[i][1]<<" , "<<tree_edge[i][2]<<" > ::"<<tree_edge[i]
[3]<<endl;
}
int main()
{
prims obj;
obj.input();
obj.algorithm();
obj.output();
return 0;
}
OUTPUT
Enter the no. of nodes in the undirected weighted graph :: 5
Enter the weights for the following edges ::
< 1 , 2 > ::6
< 1 , 3 > ::5
< 1 , 4 > ::1
< 1 , 5 > ::5
< 2 , 3 > ::3
< 2 , 4 > ::6
< 2 , 5 > ::4
< 3 , 4 > ::5
< 3 , 5 > ::2
< 4 , 5 > ::6
KRUSKAL's ALGORITHM
Enter number of nodes
4
Enter number of edges
6
enter an edge and wt
124
enter an edge and wt
136
enter an edge and wt
147
enter an edge and wt
4 3 10
enter an edge and wt
425
enter an edge and wt
231
#include<stdio.h>
#include<math.h>
#define QUEENNO 8
void main()
{
int x[QUEENNO],i;
printf("The 8 Queens problem");
placeQueen(0,x);
printf("end");
}
OUTPUT
The 8 Queens problem
12345678
1 Q-------
2 ----Q---
3 -------Q
4 -----Q--
5 --Q-----
6 ------Q-
7 -Q------
8 ---Q----
Want to see more? [n->stop, other->continue]: n