Professional Documents
Culture Documents
Merged
Merged
No:1(a)
IMPLEMENT AND ANALYZE SORTING ALGORITHMS:
Dt: SELECTION SORT AND BUBBLE SORT
SELECTION SORT
AIM:
To develop a c program for the implementation selection sort.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10],n,i,j,min,x,temp;
clrscr( );
printf(“Enter the limit:”);
scanf(“%d”,&n);
printf(“Enter the array elements are:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
min=a[i];
x=i;
for(j=i;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
x=j;
}
}
temp=a[i];
a[i]=a[x];
a[x]=temp;
}
printf(“sorting elements are:\n”);
for(i=0;i<n;i++)
{
printf (“%d\n”,a[i]);
}
getch( );
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program for selection sort is implemented successfully.
Ex. No:1(b)
IMPLEMENT AND ANALYZE SORTING ALGORITHMS:
Dt: SELECTION SORT AND BUBBLE SORT
BUBBLE SORT
AIM:
To develop a c program for the implementation bubble sort.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[100],n,i,j,swap;
clrscr( );
printf(“Enter the limit:”);
scanf(“%d”,&n);
printf(“Enter the array elements are:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
swap=a[i];
a[i]=a[j];
a[j]=swap;
}
}
}
printf(“sorting elements are:\n”);
for(i=0;i<n;i++)
{
printf (“%d\n”,a[i]);
}
getch( );
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program for bubble sort is implemented successfully.
Ex. No:2(a)
IMPLEMENT AND ANALYZE SEARCHING
Dt: ALGORITHMS:
SEQUENTIAL SEARCH AND BINARY SEARCH
SEQUENTIAL SEARCH
AIM:
To develop a c program for the implementation Sequential search.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[100],n,i,key;
clrscr( );
printf(“Enter the limit:”);
scanf(“%d”,&n);
printf(“Enter the array elements are:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Enter the number to be searched:”);
scanf(“%d”,&key);
for(i=0;i<n;i++)
if(a[i]==key)
{
printf(“Searching array element=\n a[%d] = %d \n ”,i,a[i]);
break;
}
if(a==n)
{
printf (“The no. is not in Array \n”);
}
getch( );
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program for Sequential search is implemented successfully.
Ex. No:2(b)
IMPLEMENT AND ANALYZE SEARCHING
Dt: ALGORITHMS:
SEQUENTIAL SEARCH AND BINARY SEARCH
BINARY SEARCH
AIM:
To develop a c program for the implementation Binary search.
ALGORITHM:
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program for Binary search is implemented successfully.
Ex. No: 3
IMPLEMENT AND ANALYZE RECURSIVE ALGORITHMS
Dt:
AIM:
To develop a c program for the implementation Recursive Algorithms by
merge sort.
ALGORITHM:
Merge sort (left, right)
//Input : An array
If left>right
Return
mid=(left+right)/2
end.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void partition(int [ ], int, int );
void mergesort(int [ ], int, int, int);
int main(){
int a[10],n,i;
printf("Enter the limit:");
scanf("%d",&n);
printf("Enter the array elements are:");
for(i=0;i<n;i++){
scanf("\n%d",&a[i]);
}
partition(a,0,n-1);
printf("after merge sort:\n");
for(i=0;i<n;i++){
printf("\n%d",a[i]);
}
return 0;
}
void partition (int a[ ], int low, int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(a,low,mid);
partition(a,mid+1,high);
mergesort(a,low,mid,high);
}
}
void mergesort(int a[ ], int low, int mid, int high){
int l,mi,k,lo,t[20];
lo=low;
l=low;
mi=mid+1;
while((lo<=mid)&&(mi<=high)){
if(a[10]<=a[mi]){
t[l]=a[lo];
lo++;
}
else{
t[l]=a[mi];
mi++;
}
l++;
}
if(lo>mid){
for(k=mi;k<=high;k++){
t[l]=a[k];
l++;
}
}
else{
for(k=lo;k<=mid;k++){
t[l]=a[k];
l++;
}
}
for(k=low;k<=high;k++){
a[k]=t[k];
}
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement Recursive Algorithm by merge sort
using C was executed and verified.
Ex. No: 4
IMPLEMENT AND ANALYZE BRUTE- FORCE STRING
Dt: MATCHING PROBLEM
AIM:
To develop a c program for the implementation Brute force String
matching problem.
ALGORITHM:
ALGORITM Brute force String match (T[0…n-1],P[0…m-1])
//Output: The index of the first character in the text that starts a.
j←0
j←j+1
if j=m return i
return -1
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int match(char [100],char [100],int l1,int l2);
char a[100],b[100];
void main(){
int i,l1=0,l2=0,res=0;
clrscr();
printf("Enter the string 1:");
scanf("%s",&a);
printf("Enter the string 2:");
scanf("%s",&b);
l1=strlen(a);
l2=strlen(b);
res=match(a,b,l1,l2);
if(res==-1){
printf("\npattern not found");
}
else{
printf("\n pattern is found at position=%d",res+1);
}
getch();
}
int match(char a[100],char b[100],int l1,int l2){
int i,j=0;
for(i=0;i<=l1-l2;i++){
while(j<l2 && b[j]==a[i+j]){
j=j+1;
if(j==l2){
return i;
}
}
}
return -1;
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
AIM:
To develop a c program for the implementation Min – Max by divide and
conquer approach.
ALGORITHM:
Rec _ Min _ Max (A[0…n-1])
if (start==end)
Max =A[start]
Min=A[start]
PROGRAM:
#include<stdio.h>
#include<conio.h>
int max, min;
int a[100];
void maxmin(int i, int j)
{
int max1,min1,mid;
if(i==j)
{
max=min=a[i];
}
else
{
if(i==j-1)
{
if(a[i]<a[j])
{
max = a[j];
min = a[i];
}
else
{
max = a[i];
min = a[j];
}
}
else
{
mid=(i+j)/2;
maxmin(i,mid);
max1=max;
min1=min;
maxmin(mid+1, j);
if(max<max1)
{
max=max1;
}
if(min >min1)
{
min=min1;
}
}
}
}
void main ()
{
int i, n;
clrscr();
printf ("\nEnter the limit : ");
scanf ("%d",&n);
printf ("Enter the array numbers are : \n");
for (i=1;i<=n;i++)
{
scanf ("%d",&a[i]);
}
max = a[0];
min = a[0];
maxmin(1,n);
printf ("Minimum element is: %d\n", min);
printf ("Maximum element is : %d\n", max);
getch();
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
AIM:
To develop a c program for the implementation of Multistage graph.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
int i;
int stages,stages_vertices[MAX],c[MAX][MAX];
int cost[MAX],p[MAX],n;
Get_min(int,int);
void Forward(int);
int create();
void display();
int Get_min(int s,int n)
{
int min=9999;
int min_vertex;
for(i=0;i<n;i++)
{
if(min>(c[s][i]+cost[i]))
{
min=c[s][i]+cost[i];
min_vertex=i;
}
}
return min_vertex;
}
void Forward(int n)
{
int i,r;
int d[20];
for(i=0;i<n;i++)
cost[i]=0;
for(i=n-2;i>=0;i--)
{
r=Get_min(i,n);
cost[i]=c[i][r]+cost[r];
d[i]=r;
}
p[0]=0;
p[stages-1]=n-1;
for(i=1;i<stages-1;i++)
p[i]=d[p[i-1]];
}
int create()
{
int i,j,m,p,no_of_vertices=0;
printf("\nEnter no of vertices:");
scanf("%d",&no_of_vertices);
printf("\nEnter no of stages:");
scanf("%d",&stages);
for(i=0;i<no_of_vertices;i++)
for(j=0;j<no_of_vertices;j++)
c[i][j]=9999;
for(i=0;i<stages;i++)
{
printf("\nEnter no of vertices in stage%d:",i+1);
scanf("%d",&stages_vertices[i]);
}
i=0;
j=stages_vertices[0];
for(m=0;m<stages;m++)
{
j=i+stages_vertices[m];
for(i=0;i<j;i++)
{
for(p=0;p<stages_vertices[m+1];p++)
{
printf("\nEnter cost for%d to%d:",i+1,p+j+1);
scanf("%d",&c[i][p+j]);
}
}
}
return no_of_vertices;
}
void display()
{
int i;
printf("Shortest path is...\n");
for(i=0;i<stages-1;i++)
printf("%d",p[i]+1);
printf("%d",p[i]+1);
}
void main()
{
int n;
clrscr();
n=create();
clrscr();
Forward(n);
display();
getch();
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement the multistage graph has been executed and
verified.
Ex. No:7
IMPLEMENT AND ANALYZE ALL PAIR SHORTEST
Dt: PATH USING DYNAMIC PROGRAMMING
AIM:
To develop a c program for the All pair shortest path using Dynamic
programming.
ALGORITHM:
Floyd (W[1…n,1….n])
Implements Floyd’s algorithm for the all pair shortest paths problem.
Input: The weight matrix W of a graph with no negative-length cycle.
Output: The distance matrix of the shortest paths lengths.
D ← W // is not necessary if W can be overwritten
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
D[i, j] ← min [D[i,j],D[i,k]+D[k,j]]
return D
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define INF 999
#define nV 4
int i,j;
void printMatrix(int matrix[][nV]);
void floydWarshall(int graph[][nV])
{
int matrix[nV][nV],i,j,k;
for(i=0;i<nV;i++)
for(j=0;j<nV;j++)
matrix[i][j]=graph[i][j];
for(k=0;k<nV;k++)
{
for(i=0;i<nV;i++)
{
for(j=0;j<nV;j++)
{
if(matrix[i][k]+matrix[k][j]<matrix[i][j])
matrix[i][j]=matrix[i][k]+matrix[k][j];
}
}
}
printMatrix(matrix);
}
void printMatrix(int matrix[][nV])
{
for(i=0;i<nV;i++)
{
for(j=0;j<nV;j++)
{
if(matrix[i][j]==INF)
printf("%4s","INF");
else
printf("%6d",matrix[i][j]);
}
printf("\n");
}
}
void main()
{
int graph[nV][nV]={{0,3,INF,5},{2,0,INF,4},{INF,1,0,INF},{INF,INF,2,0}};
clrscr();
floydWarshall(graph);
getch();
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement all pair shortest path was executed and verified.
Ex. No:8
IMPLEMENT AND ANALYZE KNAPSACK PROBLEM
Dt: USING GREEDY METHOD
AIM:
To develop a c program to implement knapsack problem using Greedy
method
ALGORITHM:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement knapsack problem using Greedy Method
was executed and verified.
Ex. No: 9
IMPLEMENT AND ANALYZE SUM OF SUBSETS USING
Dt: BACKTRACKING APPROACH
AIM:
To develop a c program to implement Sum of subsets using Back tracking
methodology.
ALGORITHM:
sum of subset(s,k,r)
{
X[k]=1;
if(s+W[k]=m) then write(X[1:k]);
else if (s+W[k]+W[k+1]<=m)
then sumofsubset(s+W[k],k+1,r-W[k]);
if((s=r-W[k]>=m)and(s+W[k+1]<=m)) then
{
X[k]=0;
sumofsubset(s,k+1,r-W[k]);
}
PROGRAM:
#include<stdio.h>
#include<conio.h>
int s[10],x[10],d;
void sumofsub(int,int,int);
void main()
{
int n,sum=0;
int i;
clrscr();
printf("\nEnter the number of the subset: ");
scanf("%d",&n);
printf("Enter the number of subset increasing order\n");
for(i=1;i<=n;i++)
{
scanf("%d",&s[i]);
}
printf("\n Enter the value of d:");
scanf("%d",&d);
for(i=1;i<=n;i++)
{
sum=sum+s[i];
if(sum<d&&s[1]>d)
{
printf("no subset is possible: ");
}
else
{
sumofsub(0,1,sum);
}
getch();
}
}
else
{
if(m+s[k]+s[k+1]<=d)
sumofsub(m+s[k],k+1,r-s[k]);
if((m+r-s[k]>=d)&&(m+s[k+1]<=d))
{
x[k]=0;
sumofsub(m,k+1,r-s[k]);
}
}
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement Sum of Subsets using Back Tracking approach
was executed and verified.
Ex. No: 10
IMPLEMENT AND ANALYZE TRAVELLING
Dt: SALESMAN PROBLEM USING BRANCH AND BOUND
AIM:
To develop a c program for the implementation travelling salesman problem using
branch and bound.
ALGORITHM:
For each city i, 1 <= i <=n, find the sum of the distances from city i to the two
nearest cities.
Compute the sum s of these n numbers
Divide the result by 2
If all the distances are integers, round up the result to the nearest Integer
lb = [ s/2 ]
PROGRAM:
#include<stdio.h>
#include<conio.h>
int a[10][10],visited[10],n,cost=0;
int lease(int);
void get()
int i,j;
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
visited[i]=0;
for(i=0;i<n;i++)
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
int i,ncity;
visited[city]=1;
printf("%d->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=a[city][ncity];
return;
mincost(ncity);
int least(int c)
int i,nc=999;
int min=999,kmin;
for(i=0;i<n;i++)
if((a[c][i]!=0)&&(visited[i]==0))
if(a[c][i]<min)
min=a[i][0]+a[c][i];
kmin=a[c][i];
nc=i;
if(min!=999)
cost+=kmin;
return nc;
void put()
printf("%d",cost);
void main()
clrscr();
get();
mincost(0);
put();
getch();
}
OUTPUT:
CRITERIA TOTAL
PROGRAM & /10
EXECUTION
OUTPUT&RESULT /10
TOTAL /20
RESULT:
Thus the program to implement the travelling salesman problem using branch and
bound has been executed and verified.