Beruflich Dokumente
Kultur Dokumente
ALGORITHM :
BINARY SEARCH
LINEAR SERACH
#include<stdio.h>
#include<conio.h>
int a[20] , n , k ; /* variable declaration */
int bsearch ( int begin , int end ) ; /* Function declaration */
void main ()
{
int i , flag = 0 ;
clrscr () ;
printf ( " \n Enter size of the array n : " ) ;
scanf ( "%d" , &n ) ;
printf ( " \n Enter elements of array in ascending order : " ) ;
for ( i = 0 ; i < n ; i++ )
scanf ( "%d" , &a[i] ) ;
printf ( "\n Enter the key element : " ) ;
scanf ( "%d" , &k ) ;
flag = bsearch ( 0, n - 1 ) ;
if ( flag == 1 )
printf ( " \n Successful search , key element is present " ) ;
else
printf ( " \n Unsuccessful search " ) ;
getch () ;
}
==========Input - Output=============
Unsuccessful search
# include <stdio.h>
# include <conio.h>
void main()
{
int n, a[20], key;
int pos;
int k;
clrscr();
printf("\n Enter How many Numbers : ");
scanf("%d", &n);
printf("\n Enter %d Numbers : \n ", n);
for(k=1; k<=n; k++)
scanf("%d", &a[k]);
printf("\n Enter the Key to be Searched : ");
scanf("%d", &key);
if( pos == -1 )
printf("\n Key Not Found");
else
printf("\n Key %d Found in position %d", key, pos);
getch();
}
==========Input - Output=============
ALGORITHM :
#include<stdio.h>
#include<conio.h>
#define max 100
void heapify () ;
void heapsort () ;
int maxdel ();
int a[max] , b[max] ,n ;
void main ()
{
int i ;
int m ;
clrscr () ;
printf ( " \n Enter array size : " ) ;
scanf ( "%d" , &n ) ;
m=n;
printf ( " \n Enter elements : \n " ) ;
for ( i = 1 ; i <= n ; i++ )
scanf ( "%d" , &a[i] ) ;
heapsort () ;
printf ( " \n The sorted array is : \n " ) ;
for ( i = 1 ; i <= m ; i++ )
printf ( "\n%d" , b[i] ) ;
getch () ;
}
void heapsort ()
{
int i ;
heapify () ;
for ( i = n ; i >= 1 ; i-- )
b[i] = maxdel () ;
}
void heapify ()
{
int i , e , j ;
// start from middle of a and move up to 1
while ( j <= n )
{
if ( j < n && a[j] < a[j+1] )
j++ ; //pick larger of children
if ( e >= a[j] )
break; // is a max heap
a[j/2] = a[j] ;
j=j*2; //go to the next level
}
a[j/2] = e ;
}
}
int maxdel ()
{
int x , j , e , i ;
if ( n == 0 )
return -1 ;
x = a[1] ; //save the maximum element
e = a[n] ; //get the last element
n-- ;
// Heap the structure again
i=1;
j = 2;
while ( j <= n )
{
if ( j < n && a[j] < a[j+1] )
j++ ; //Pick larger of two childrean
if ( e >= a[j] )
break ; //subtree is heap
a[i] = a[j] ;
i=j;
j=j*2; // go to the next level
}
a[i] = e ;
return x ;
}
Enter elements :
7
1
9
3
5
1
3
5
7
9
ALGORITHM :
Mergesort ( A [ 0…..n-1 ] )
PROGRAM :
# include <stdio.h>
# include <conio.h>
void main()
{
int n, a[20];
int k;
Department of CSE / ISE 12 Analysis and Design of
Algorithms
clrscr();
printf("\n Enter How many Numbers : ");
scanf("%d", &n);
printf("\n Enter %d Numbers : \n ", n);
for(k=1; k<=n; k++)
scanf("%d", &a[k]);
MergeSort(a, 1, n);
getch();
}
Enter 5 numbers :
99
67
85
12
97
12
67
85
97
99
ALGORITHM :
DFS ( G )
# include <stdio.h>
# include <conio.h>
# define INFINITY 999
# define TRUE 1
# define FALSE 0
int StackEmpty()
{
return( top==-1 );
}
void Push(int x)
{
top=top+1;
s[top]=x ;
}
int Pop()
{
int x = s[top] ;
top=top-1;
return(x);
}
if(flag==FALSE)
printf("\n None");
}
void main()
{
int n, g[20][20];
int sv;
clrscr();
printf("\n Enter How many nodes : ");
scanf("%d", &n);
InitGraph(g,n);
ReadGraph(g);
printf("\n Eneter the Starting Vertex : ");
scanf("%d", &sv);
DFS(g, n, sv);
getch();
}
< 1 ,2 > 1
1 2
< 2 ,1 > 1
< 2 ,3 > 1
< 2 ,4 > 1
< 3 ,2 > 1
<3,4> 1
4 3
< 4 ,2 > 1
< 4 ,3 > 1
Connected
< 1 ,2 > 1
1 2
< 1 ,3 > 1
< 2 ,1 > 1
< 3 ,1 > 1
Not Connected 3 4
ALGORITHM :
#include<stdio.h>
#include<conio.h>
int a[20] , n ;
void main ()
{
void selectionsort() ;
int i ;
clrscr () ;
printf ( " \n Enter size of the array : " ) ;
scanf ( "%d" , &n ) ;
printf ( " \n Enter the elements : \n " ) ;
for ( i = 0 ; i < n ; i++ )
scanf ( "%d" , &a[i] ) ;
selectionsort () ;
printf ( " \n The sorted elements are : " ) ;
for ( i = 0 ; i < n ; i++ )
printf ( "\n%d" , a[i] ) ;
getch () ;
}
void selectionsort ()
Department of CSE / ISE 22 Analysis and Design of
Algorithms
{
int i , j , min , temp ;
for ( i = 0 ; i < n - 1 ; 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 ;
}
}
==============Input - Output=============
PROGRAM :
# include <stdio.h>
# include <conio.h>
# define INFINITY 999
# define TRUE 1
# define FALSE 0
int StackEmpty()
{
return( top==-1 );
}
void Push(int x)
{
top=top+1;
s[top]=x ;
}
Department of CSE / ISE 24 Analysis and Design of
Algorithms
int Pop()
{
int x = s[top] ;
top=top-1;
return(x);
}
flag=TRUE;
for(k=1; k<=n; k++)
if (visited[k] == FALSE)
flag=FALSE;
if(flag==TRUE)
printf("\n Topological Order Found");
else
printf("\n No Topological Order Found");
}
void main()
{
int n, g[20][20];
clrscr();
printf("\n Enter How many nodes : ");
scanf("%d", &n);
InitGraph(g,n);
ReadGraph(g);
TopoOrder(g, n);
getch();
}
============Input – Output============
100 1
1 100
ALGORITHM :
Insertionsort ( A [ 0…n-1 ] )
#include<stdio.h>
#include<conio.h>
int a[20] , n ;
void main ()
{
void insertionsort() ;
int i ;
clrscr () ;
printf ( " \n Enter size of the array : " ) ;
scanf ( "%d" , &n ) ;
printf ( " \n Enter the elements : \n " ) ;
for ( i = 0 ; i < n ; i++ )
scanf ( "%d" , &a[i] ) ;
insertionsort () ;
printf ( " \n The sorted elements are : " ) ;
for ( i = 0 ; i < n ; i++ )
printf ( "\n%d" , a[i] ) ;
void insertionsort ()
{
int i , j , min ;
for ( i = 0 ; i < n ; i++ )
{
min = a[i] ;
j=i-1;
while ( j >= 0 && a[j] > min )
{
a[j + 1] = a[j] ;
j=j-1;
}
a[j + 1] = min ;
}
}
=============Input - Output=============
ALGORITHM :
Knapsack ( i, j )
PROGRAM :
# include <stdio.h>
# include <conio.h>
int m;
int n;
float w[30];
float p[30];
float x[30];
float totprof ;
void main()
{
int k;
float p1,p2, wt;
clrscr();
printf("\n Enter the Knapsack Capacity : ");
scanf("%d", &m);
printf("\n Enter the number of Objects : ");
scanf("%d", &n);
for(k=1; k<=n; k++)
{
printf("\n Enter the Weight & Profit for Object %d : ", k);
scanf("%f%f", &w[k], &p[k]);
}
wt=0;
for(k=1; k<=n-1; k++)
{
p1=DKnapSack(k, m-wt);
p2=DKnapSack(k+1, m-wt);
if(p1==p2)
x[k] = 0;
else
{ x[k] = 1; wt=wt+w[k]; }
}
p1=DKnapSack(n, m-wt);
x[n] = (p1==0) ? 0 : 1 ;
===========Input – Output=========
Optimal solution = 34
ALGORITHM :
Dijkstra( G , s )
PROGRAM :
# include <stdio.h>
# include <conio.h>
}
Department of CSE / ISE 37 Analysis and Design of
Algorithms
void ReadGraph(int g[20][20])
{
int i, j, wt; char ch;
do
{
printf("\n Input Directed Edge <v1, v2, Wt> : ");
scanf("%d%d%d", &i, &j, &wt);
g[i][j] = wt;
//g[i][j] = g[j][i] = wt ;
printf(" One More Edge ? (y/n) : ");
ch=getche();
}while(ch=='y');
}
selected[sv]=TRUE;
dist[sv]=0;
void main()
{
int n, g[20][20], sv, dist[20];
int k;
clrscr();
printf("\n Enter How many nodes : ");
scanf("%d", &n);
InitGraph(g,n);
ReadGraph(g);
getch();
}
0 3 100 7 100
3 0 4 2 100 2 3
100 4 0 5 6
7 2 5 0 4
100 100 6 4 0
1 4 5
Enter starting vertex : 1
2 : 3
3 : 7
4 : 5
Department of CSE / ISE 40 Analysis and Design of
Algorithms
5 : 9
ALGORITHM :
Quicksort ( A [l…r] )
Partition ( A [ l……r ] )
PROGRAM :
# include <stdio.h>
# include <conio.h>
void main()
{
int n, a[20];
int k;
clrscr();
printf("\n Enter How many Numbers : ");
scanf("%d", &n);
printf("\n Enter %d Numbers : \n ", n);
for(k=1; k<=n; k++)
scanf("%d", &a[k]);
QuickSort(a, 1, n);
printf("\n Sorted Numbers are : \n ");
for(k=1; k<=n; k++)
printf("%5d", a[k]);
getch();
}
===============Input – Output=============
4 2 1 9 8 3 5 7 10 6
1 2 3 4 5 6 7 8 9 10
ALGORITHM :
Kruskal ( G )
PROGRAM :
#include<conio.h>
#include<stdio.h>
struct tag
{
int v1 , v2 , wt ;
};
typedef struct tag edge ;
temp = e[j] ;
e[j] = e[j+1] ;
e[j+1] = temp ;
}
}
void main ()
{
edge e1[30] , e2[30] ;
int n1 , n2 , n , cost ;
clrscr () ;
printf ( " \n Enter how many nodes : \n " ) ;
scanf ( "%d" , &n ) ;
n2 = n - 1 ;
printf ( " \n Enter how many edges of the graph : \n " ) ;
scanf ( "%d" , &n1 ) ;
Department of CSE / ISE 47 Analysis and Design of
Algorithms
readgraph ( e1 , n1 ) ;
cost = kruskal ( e1 , n1 , e2 , n2 ) ;
printf( " \n minimum cost = %d \n " , cost ) ;
printf ( " \n the spanning tree is : \n " ) ;
printgraph ( e2 , n2 ) ;
getch () ;
}
=============Input - Output============
minimum cost = 99
1 <......> 6 : 10
3 <......> 4 : 12
2 <......> 7 : 14
2 <......> 3 : 16
4 <......> 5 : 22
5 <......> 6 : 25
ALGORITHM :
PROGRAM :
# include <stdio.h>
# include <conio.h>
# define INFINITY 999
# define TRUE 1
# define FALSE 0
int QueueEmpty()
{
return( front==-1 && rear==-1 );
}
void QInsert(int x)
{
rear = rear + 1;
q[rear] = x ;
if( front==-1)
front=front+1;
}
int QDelete()
{
int x = q[front] ;
if( front == rear )
front = rear = -1;
else
front = front + 1;
return(x);
}
void main()
{
int n, g[20][20];
int sv;
clrscr();
printf("\n Enter How many nodes : ");
scanf("%d", &n);
InitGraph(g,n);
ReadGraph(g);
printf("\n Eneter the Starting Vertex : ");
scanf("%d", &sv);
BFS(g, n, sv);
getch();
}
===============Input – Output=============
2
3
4
PROGRAM :
void main()
{
int n, g[20][20], p[20][20];
clrscr();
printf("\n Enter How many nodes : ");
scanf("%d", &n);
InitGraph(g,n);
ReadGraph(g);
AllPairs(g, n, p);
getch();
}
0 100 3 100 2
2 0 100 100
1 2
100 7 0 1 7
6 100 100 0
3
The final Adjacency matrix is : 6
0 10 3 4 3 4
2 0 5 6 1
7 7 0 1
6 16 9 0
ALGORITHM :
Backtrack ( X [ 1….i ] )
#include<stdio.h>
#include<conio.h>
#define max 10
1 2 6
1 8
============Input - Output=============
5 10 15
5 12 13
12 18
ALGORITHM :
ShiftTable ( P [ 0….m-1 ] )
//Fills the shift table used by Horspool’s and Boyer – Moore algorithms.
//Input: Pattern P [ 0…m-1 ] and an alphabet of possible characters.
//Output: Table [ 0…size – 1 ] indexed by the alphabet’s characters and
filled with shift sizes computed by formula.
Step 1: for j = 0 to m –2 do Table [ P [ j ] ] = m – 1 – j
Step 2: Table.
Horspool Algorithm :
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 256
int horspool ( char * , char * ) ;
int t[max] ;
void shifttable ( char [] ) ;
void main ()
{
char source[max] ;
char pattern[max] ;
int found ;
clrscr () ;
printf ( " \n Enter source string : " ) ;
gets ( source ) ;
printf ( " \n Enter pattern string : " ) ;
gets ( pattern ) ;
found = horspool ( source , pattern ) ;
if ( found == -1 )
printf ( " \n Pattern not found " ) ;
else
printf ( " \n pattern found at : %d \n " , found + 1 ) ;
getch () ;
}
pattern found at : 4
=============Input - Output============
ALGORITHM :
Binomial ( n , k )
#include<stdio.h>
#include<conio.h>
int bc ( int , int ) ;
void main ()
{
int n , k ;
clrscr () ;
printf ( " \n enter the values of n & k : " ) ;
scanf ( "%d%d" , &n , &k );
if ( n < k )
printf ( " \n invalid input " ) ;
else
printf ( " \n %dC%d = %d \n " , n , k , bc ( n , k ) ) ;
getch () ;
}
5C3 = 10
===============Input - Output============
invalid input
ALGORITHM :
Prim ( G )
# include <stdio.h>
# include <conio.h>
# define INFINITY 999
# define TRUE 1
# define FALSE 0
included[1] = TRUE ;
for(k=2; k<=n; k++)
included[k] = FALSE ;
mincost = 0;
for(k=1; k<=n-1; k++)
{
min = INFINITY; u=1; v=1;
for(i=1; i<=n; i++)
if(included[i]==TRUE)
for(j=1; j<=n; j++)
if( g[i][j] < min )
{
min = g[i][j];
u = i;
v = j;
}
t[u][v] = t[v][u] = g[u][v] ;
mincost = mincost + g[u][v] ;
included[v] = TRUE;
printf("\n <%d, %d> = %d", u, v, t[u][v]);
void main()
{
int n, g[20][20], t[20][20];
int cost;
clrscr();
printf("\n Enter How many nodes : ");
scanf("%d", &n);
InitGraph(g,n);
InitGraph(t,n);
ReadGraph(g);
Department of CSE / ISE 72 Analysis and Design of
Algorithms
printf("\n The order of Insertion of edges :\n");
cost = PrimAlg(g,n,t);
printf("\n\n Minimum cost = %d\n", cost);
printf("\n The Spanning Tree is \n");
PrintGraph(t,n);
getch();
}
===========Input – Output==========
Total weight = 7 3
ALGORITHM :
DFS ( G )
# include <stdio.h>
# include <conio.h>
# define INFINITY 999
# define TRUE 1
# define FALSE 0
int StackEmpty()
{
return( top==-1 );
}
void Push(int x)
{
top=top+1;
s[top]=x ;
}
int Pop()
{
int x = s[top] ;
top=top-1;
return(x);
}
g[i][j] = wt;
printf(" One More Edge ? (y/n) : ");
ch=getche();
}while(ch=='y');
}
if(flag==FALSE)
printf("\n None");
}
void main()
{
int n, g[20][20];
int sv;
clrscr();
printf("\n Enter How many nodes : ");
scanf("%d", &n);
InitGraph(g,n);
ReadGraph(g);
printf("\n Eneter the Starting Vertex : ");
scanf("%d", &sv);
DFS(g, n, sv);
getch();
}
ALGORITHM :
# include <stdio.h>
# include <conio.h>
# define TRUE 1
# define FALSE 0
void main()
{
int n, g[20][20], p[20][20];
int res;
clrscr();
printf("\n Enter How many nodes : ");
scanf("%d", &n);
printf("\n Enter the graph in matrix forrmat : \n");
ReadGraph(g,n);
res = WarshallAlg(g, n, p);
printf("\n Path Matrix : \n");
PrintGraph(p,n);
if(res==TRUE)
printf("\n Strongly/Weakly Connected Graph");
else
printf("\n Disconnected Graph");
getch();
}
ALGORITHM :
Backtrack ( X [ 1….i ] )
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define TRUE 1
# define FALSE 0
}
}
void main()
{
int board[20][20], n;
clrscr();
printf("\n Enter the Number of Queens(Power of 2) : ");
scanf("%d", &n);
NQueens(board, n);
Display(board, n);
getch();
}
X Q X X
X X X Q
Q X X X
X X Q X
X X Q X
Q X X X
X X X Q
X Q X X
==========Input – Output=========
Q X X X X X X X
X X X X Q X X X
X X X X X X X Q
Department of CSE / ISE 86 Analysis and Design of
Algorithms
X X X X X Q X X
X X Q X X X X X
X X X X X X Q X
X Q X X X X X X
X X X Q X X X X
Q X X X X X X X
X X X X X Q X X
X X X X X X X Q
X X Q X X X X X
X X X X X X Q X
X X X Q X X X X
X Q X X X X X X
X X X X Q X X X