You are on page 1of 44

Ex.

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:

\\ sorts a given array by selection sort


ALGORITHM selection sort (A[0…n-1])
{
\\ Input: An array A[0…n-1] of elements
\\ Output; Array A[0…n-1] in sorted order
for i←0 to n-2 do min←i
for j←i+1 to n-1do
ifA[j]<A[Min]
min←j
swap A[i] and A[min]
end for
}

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:

ALGORITHM selection sort (A[0…n-1])


\\ sorts a given array by bubble sort
\\ Input: An array A[0…n-1] of elements
\\ Output; Array A[0…n-1] in sorted order
for i←0 to n-2 do min←i
for j←0 to n-2-I do
ifA[j+1]<A[j}
swap A[i] and A[j+1]

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:

INPUT: list of size N. Target value T


OUTPUT: position of T in list I.
BEGIN
Set found to false
Set I to 0
While (I==N) and (Found is false)
If list (I) = T
Found = True
Else
I=I+1
End
if found is false
I is not present in list
END

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:

ALGORITHM Binary search


INPUT: list of elements to be given as input
OUTPUT: Displaying the searched elements
A← sorted array
n← Size of array
x← Value to be searched
set lower bound = 1
set upper bound = n
while x not found
if upper bond < low bound
EXIT; x does not exists
set mid point = lower bound+(upper bound-lower bound)/2
if A[mid point]<x
set lower bound = midpoint+1
if A[mid point]>x
set upper bound = mid point-1
if A[mid point]=x
EXIT: x found at location mid point
End while
END
PROGRAM:
#include<stdio.h>
#include<conio.h>
void search();
void main( ){
int a[100],n,i,key,mid,low,high;
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);
void search(){
int low=0
high=n-1;
mid=(low+high)/2;
if(a[mid]==key){
printf{"\n Found the location=%d",mid);
}
else if(key<mid){
n=mid;
serach();
}
else if(key>mid){
low=mid+1;
search();
}
else{
printf("\n Value not found");
}
getch( );
}

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)

//Implement merge sort with a search key

//Input : An array

//Output: The index of the first element in A[0..n-1]

//or-1 if no such element is found

Merge sort (arr,left,right)

If left>right

Return

mid=(left+right)/2

merge sort (arr, left, mid)

merge sort (arr, mid+1,right)

merge (arr, left, mid, right)

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

//implements brute force using matching

//Input: An array T[0…n-1] of n characters representing a test and an


array

P[0…m-1] of m character representing a pattern.

//Output: The index of the first character in the text that starts a.

//Matching substring or -1 if the search is unsuccessful.

for i←0 to n-m do

j←0

while j<m and P[j]=T[i+j] do

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:

Thus the program to implement Brute-force string matching using c


was executed and verified.
Ex. No: 5
IMPLEMENT AND ANALYZE MIN-MAX ALGORITHM
Dt: USING DIVIDE AND CONQUER APPROACH

AIM:
To develop a c program for the implementation Min – Max by divide and
conquer approach.

ALGORITHM:
Rec _ Min _ Max (A[0…n-1])

//Return min and max element by finding using recursive call

//Input: An array A[0…n-1] of elements

//Output: Min _ Minimum element and max _ Maximum elements.

int[ ] find MinMax (int A[], int Start, int end)

int max, min;

//case 1: only one element in the given array

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:

Thus the min-max Algorithm using Divide and conquer approach


using c implemented successfully.
Ex. No:6
IMPLEMENT AND ANALYZE MILTISTAGE GRAPHS
Dt: USING DYNAMIC PROGRAMMING APPROACH

AIM:
To develop a c program for the implementation of Multistage graph.

ALGORITHM:

F graph (graph G, int k, int n, int p[])


{
float cost [max size], int d[max size], r;
cost[n]=0.0
for (int j=n-1;j>=1;j--)
{
Let r be a vertex such that is an edge of G and c[j][r]+cosr[r] is minimum;
Cost[j] = c[j][r] + cost [r]; D[j]=r;
}
P[1] =1,p[k]=n
for(j=2;j<k-1;j++)
p[j] = d[p(j-1)];
}

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:

Fractional knapsack (S,W);


Input: Set S of items, such that each item i ∈ s has a positive benefit bi
and a positive weight wi; positive maximum total weight W.
Output: Amount xi of each item i ∈ s that maximizes the total benefit
while not exceeding the maximum total weight W.
for each item i ∈ s do
xi ← 0
𝒃𝒊
vi ← 𝒘𝒊 {value index of item i}
w←0 {total weigth}
while w < W do
remove from S an item i with highest value index
a ← min{wi,W-w} (more than W-w causes a weight overflow)
xi ← a
w ← w+a
PROGRAM:
#include<stdio.h>
#include<conio.h>
void knapsack(float capacity, int n, float weight[], float profit[])
{
float x[20], totalprofit,y;
int i,j;
y=capacity;
totalprofit=0;
for(i=0;i<n;i++)
x[i]=0.0;
for(i=0;i<n;i++)
{
if(weight[i]>y)
break;
else
{
x[i]=1.0;
totalprofit=totalprofit+profit[i];
y=y-weight[i];
}
}
if(i<n)
x[i]=y/weight[i];
totalprofit=totalprofit+(x[i]*profit[i]);
printf("The selected elements are \n");
for(i=0;i<n;i++)
if(x[i]==1.0)
printf("\n Profit is %f with weight %f",profit[i],weight[i]);
else if (x[i]>0.0)
printf("\n%f part of profit %f with weight %f",x[i],profit[i],weight[i]);
printf("\n Total profit for %d objects with capacity
%f=%f\n\n",n,capacity,totalprofit);
}
void main()
{
float weight[20],profit[20],ratio[20],t1,t2,t3;
int n;
float capacity;
int i,j;
clrscr();
printf("Enter number of objects:");
scanf("%d",&n);
printf("\n Enter the capacity of knapsack:");
scanf("%f",&capacity);
for(i=0;i<n;i++)
{
printf("\nEnter %d(th) profit:",(i+1));
scanf("%f",&profit[i]);
printf("\nEnter %d(th) weigth:",(i+1));
scanf("%f",&weight[i]);
ratio[i]=profit[i]/weight[i];
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(ratio[i]>ratio[j])
{
t1=ratio[i];
ratio[i]=ratio[j];
ratio[j]=t1;
t2=weight[i];
weight[i]=weight[j];
weight[j]=t2;
t3=profit[i];
profit[i]=profit[j];
profit[j]=t3;
}
}
knapsack(capacity,n,weight,profit);
getch();
}
OUTPUT:

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

void sumofsub(int m,int k,int r)


{
int i=1;
x[k]=1;
if((m+s[k])==d)
{
printf("The subset is: ");
for(i=1;i<=k;i++)
if(x[i]==1)
printf("\t %d",s[i]);
printf("\n");
}

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;

printf("Enter no. of cities:");

scanf("%d",&n);

printf("\nEnter Cost Matrix:\n");


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

printf("\nEnter Elements of Row#:%d\n",i+1);

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

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

visited[i]=0;

printf("\nThe cost list is:\n");

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

printf("\n");

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

printf("\t%d",a[i][j]);

void mincost(int city)

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

printf("%d",cost);

void main()

clrscr();

get();

printf("\n The path is:\n");

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.

You might also like