You are on page 1of 49

DEPARTMENT OF COMPUTER SCIENCE &

ENGINEERING

Lab Manual

KCS-553

SUBJECT : Design and Analysis of Algorithms


BRANCH : CSE

FACULTY NAME: Abha Sharma


LIST OF EXPERIMENTS AS PER UNIVERSITY SYLLABUS

DESCRIPTION
1 Compute and analyze the complexity of Insertion sort Algorithm
2 Compute and analyze the complexity of Merge sort Algorithm
3 Compute and analyze the complexity of Binary Search and Linear
Search
4 Compute and analyze the complexity of Heap sort Algorithm
5 Compute and analyze the complexity of Counting sort Algorithm
6 Compute and analyze the complexity of Quick sort Algorithm
7 Compute and analyze the complexity of Radix sort Algorithm
8 Compute and analyze the complexity of Selection Algorithm
9 Compute and analyze the complexity of minimum spanning tree
using Kruskal algorithm
10 Compute and analyze the complexity of Knap Sack Problem

11 Implement N Queen Problem using Backtracking

12 Compute and analyze the complexity of Bubble sort Algorithm


13 Compute and analyze the complexity of Bucket sort Algorithm

14 Compute and analyze the complexity of All Pair shortest Path


Algorithm
1. PROGRAM TO IMPLEMENT INSERTION SORT.
#include<stdio.h>
#include<conio.h>
void main() {
int a[50],i,j,key,n;
clrscr();
printf("\n Enter how many
no:"); scanf("%d",&n);
printf("\n Enter the array
elements:"); for(i=0;i<n
;i++) scanf("%d",&a[i]);
for(j=1;j<n;j++) {
key=a[j];
i=j‐1;
while(i>=0 && a[i]>key)
{
a[i+1]=a[
i]; i=i‐1;
}
a[i+1]=key;
}
printf("\n Sorted
array is:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

OUTPUT‐

Enter how many no: 10


Enter the array elements: 18 9 14 13 16 15 19 8 10 4

Sorted array
is: 4 8 9 10
13
14 15 16 18 19
2. PROGRAM TO IMPLEMENT MERGE SORT

#include<stdio.h>

#include<conio.h>

#define max 20

voidmergesort(int a[max], intp,int


q); void merge(int
a[max],intp,intq,int r); void main()
{
inti,q,r,a[max],n;

clrscr();

printf("Enter how many elements : ");

scanf("%d",&n);

printf("\n Enter the array


elements:");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
mergesort(a,1,n);
for(i=1;i<=n;i++)
printf("\n%d",a[i]);
getch();

}
voidmergesort(int a[max], int p, int r)
{ int q;
if(p<r) {
q=(p+r)/
2;
mergesort(a,p,q);
mergesort(a,q+1,r);
merge(a,p,q,r);

}
}

void merge(int a[max],intp,intq,int r)


{
int L[max],R[max],i,j,k,n1,n2;
n1=q‐p+1;
n2=r‐q;
for(i=1;i<=n1;i
++)
L[i]=a[p+i‐1];
for(j=1;j<=n2;j
++)
R[j]=a[q+j];
L[n1+1]=32767
;
R[n2+1]=32767
; i=1;
j=1;
for(k=p;k<=r;k++)
{
if(L[i]<R[j])
{
a[k]=L[
i];
i=i+1;
}
else

a[k]=R[j];

j=j+1;
}
}
}
OUTPUT‐
Enter how many elements : 10
Enter the array elements:1 9 2 6 3 8 4 5 7 0
3 Binary Search

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int i,n,s,mid,beg,end,a[100];

printf("enter the no");

scanf("%d",&n);

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

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

printf("search no");

scanf("%d",&s);

beg=0;

end=n-1;

mid=(beg+end)/2;

while(s!=a[mid] && beg<=end)

if(s<a[mid])

end=mid+1;

else

beg=mid+1;

mid=(beg+mid)/2;

if(s==a[mid])

printf("%d",mid+1);
else
printf("not found");

getch();

// LINEAR SEARCH

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int s,j,i,n,a[100],flag;

printf("enter the number");

scanf("%d",&n);

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

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

printf("enter the search no");

scanf("%d",&s);

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

if(s==a[i])

j=i+1;

flag=1;

if(flag==0)

printf("no. is not found");


}
else

printf("%d",j);

getch();

}
4. PROGRAM TO IMPLEMENT HEAP SORT.

#include <stdio.h>
#define MAX 10
void swap(int *x,int *y) {
int temp;
temp = *x;
*x = *y; *y
= temp;
}
void adjust( int list[],inti, int n)
{
intj,k,flag;
k = list[i];
flag = 1;
j = 2 * i;
while(j <= n && flag) {
if(j < n && list[j] < list[j+1])
j++;
if( k >= list[j])
flag =0;
else {
list[j/2] = list[j];
j=j
*2;
}}
list [j/2] = k;
}
voidbuild_initial_heap( int list[], int n)
{ inti;
for(i=(n/2);i>=0;
i‐‐)
adjust(list,i,n‐1);
}
voidheapsort(int list[],int n)
{ inti;
build_initial_heap(list,
n); for(i=(n‐2); i>=0;i‐
‐)
{
swap(&list[0],&list[i+
1]); adjust(list,0,i);
}
}
voidreadlist(int list[],int n)
{
inti;

printf("Enter the
elements\n");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
}
voidprintlist(int list[],int n)
{
inti;
printf("The elements of the list are:
\n"); for(i=0;i<n;i++)
printf("%d\t",list[i]);
}
void main()
{
int list[MAX], n;
clrscr();
printf("Enter the number of elements in the list max=10\n");

scanf("%d",&n);
readlist(list,n);
printf("The list before
sorting is:\n");
printlist(list,n);
heapsort(list,n);
printf("\nThe list after
sorting is:\n");
printlist(list,n);
getch();
}

OUTPUT‐
Enter the number of elements in the list max = 10
8
Enter the elements
3 4 9 2 1 5 10 0
The list before sorting is:
The elements of the list are:
3 4 9 2 1 5 10 0
The list after sorting is:
The elements of the list are:
0 1 2 3 4 5 9 10
5. PROGRAM TO IMPLEMENT COUNTING SORT.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],c[100],n,i,k,big;
clrscr();
printf("\n how many
number:"); scanf("%d",&n);
printf("\n Enter the array
elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
big=a[1];
for(i=2;i<n;i++)
{
if(big<a[i
])
big=a[i];
}
printf("bigest=%d",bi
g);
for(i=0;i<=big;i++)
c[i]=0;
for(k=1;k<=n;k
++)
c[a[k]]=c[a[k]]+
1;
for(i=1;i<=big;i
++)
c[i]=c[i]+c[i‐1];
for(k=n;k>=1;k‐
‐)
{
b[c[a[k]]]=a[k];
c[a[k]]=c[a[k]]‐1;
}
printf("\n sorted
array:");
for(i=1;i<=n;i++)
printf(" %d\t",b[i]);
getch();
}
OUTPUT‐

How many number:10

Enter the array


elements 8 9 6 5 8 7 6
4
9 3 Biggest=9
Sorted array:
3 4 5 6 6 7 8 8 9 9
6. Wap to implement quick sort.

#include<stdio.h>
#include<conio.
h>
#define max
100 int
a[max],n,i,l,h;
void main() {
void input(void);
input();
getch();
}
void input(void)
{
voidquick_sort(int
a[],inti,int h); void
output(int a[],int n);
printf("How many elements in the
array : "); scanf("%d",&n);
printf("\n");
printf("Enter the elements
:\n"); for(i=0;i<=n‐1;i++) {
scanf("%d",&a[I]);
}
fflush(stdin);
i=0; h=n‐1;
quick_sort(a,i,h);
printf("sorted
array
:\n"); output(a,n);
fflush(stdout);
}
voidquick_sort (int a[],inti,int
h) {
inttemp,key,low,high;
low=i;
high=h;
key=a[(low+high)/
2]; do {
while(key >a[low])
{ low++;
}
while(key<a[high]
) { high‐‐;
}
if(low<=high) {
temp=a[low];
a[low++]=a[hig
h]; a[high‐‐
]=temp;
}
}
while(low<=high);

if(l<high)
quick_sort(a,l,hig
h); if(low<h)
quick_sort(a,low,
h);
}
void output(int a[],int n)
{ for(i=0;i<=n‐1;i++) {
printf("%d\n",a[i]);
}
}

OUTPUT:
How many elements in the array :
4 Enter the elements : 7 41 3 22
Sorted array :
3
7
22
41
7. Wap to implement radix sort.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={343, 562, 785, 287, 321};
int d, temp=0, i=0, j=0, k=0, u[5],
t[5], h[5]; clrscr();
for(i=0; i<5; i++)
{
u[i]=a[i]%1
0;
d=a[i]/10;
t[i]=d%10;
h[i]=d/10;
}
for(i=0; i<5; i++)
{
for(j=0; j<4; j++)
{
if(u[j+1]<u[j])
{
temp= a[j];
a[j]=a[j+1];
a[j+1]=tem
p;
}
}
}
for(j=0; j<5; j++)
{
for(j=0; j<5; j++)
{
if(u[j+1]<h[j])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=tem
p;
}
}
}
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if(h[j+1]<h[j])
{
temp= a[j];

a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0; i<5; i++)
{
printf("\n %d Sorted array :", a[i]);
}
getch();
}

OUTPU
T: Sorted
array:
287
321 343 562
785
8 Selection Sort

#include<stdio.h>

#include<conio.h>

void selectionsort(int a[],int n);

void main()

int i,n,a[10];

clrscr();

printf("enter thje size of array");

scanf("%d",&n);

printf("\nenter the element");

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

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

selectionsort(a,n);

printf("\n sorted array\n\n");

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

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

printf("\n");

getch();

void selectionsort(int a[],int n)

int temp, small,loc,i,j;

for(i=1;i<=n-1;i++)

small= a[i-1];

loc=i-1;
for(j=i;j<=n-1;j++)
{

if(a[j]<small)

small=a[j];

loc=j;

if(loc!=(i-1))

temp=a[i-1];

a[i-1]=a[loc];

a[loc]=temp;

}
9 Minimum Spaining Tree krushkal algo

#include<stdio.h>

#include<conio.h>

#define size 20

#define infinite 32767

void prim(int g[][size],int nodes)

int tree[size], i ,j,k;

int min_dist, v1,v2,total=0;

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

tree[i]=0;

printf("\n\n the minimum spaining tree is :\n");

tree[0]=1;

for(k=1;k<=nodes-1;k++)

min_dist=infinite;

for(i=0;i<=nodes-1;i++)

for(j=0;j<=nodes-1;j++)

if(g[i][j] && (( tree[i] && !tree[j])|| (!tree[i] && tree[j])))

if(g[i][j] < min_dist)

min_dist=g[i][j];

v1=i;
v2=j;
}

printf("\n edge (%d %d)and weight=%d",v1,v2,min_dist);

tree[v1]=tree[v2]=1;

total= total+min_dist;

printf("\n\n\t total path length is = %d",total);

void main()

int g[size][size],nodes;

int v1, v2, length,i,j,n;

clrscr();

printf("\n\t prims algo\n");

printf("\n enter the number of nodes in graph");

scanf("%d",&nodes);

printf("enter the number of edge in the graph");

scanf("%d",&n) ;

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

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

g[i][j]=0;

printf("\n enter the edge and weight \n");

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

printf("\nenter the edge by v1 and v2 : ");


printf("[read the graph starting node 0]") ;
scanf("%d %d",&v1,&v2);

printf("\nenter the corresponding weight :") ;

scanf("%d",&length);

g[v1][v2]=g[v2][v1] =length;

getch();

printf("\n\t");

clrscr();

prim(g,nodes);

getch();

}
10 KNAPSACK PROBLEM USING GREEDY APPROCH

#include<stdio.h>

#include<conio.h>

void knapsack(int n,float m,float w[],float p[] );

void main()

int i,j,n;

float p[15],w[15],c[15],temp,m;

clrscr();

printf("\nenter of object ");

scanf("%d",&n);

printf("\nenter the weights");

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

scanf("%f",&w[i]);

flushall();

printf("\nenter the profit");

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

scanf("%f",&p[i]);

printf("\nenter the knapsack size");

scanf("%f",&m);

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

c[i]=p[i]/w[i];

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

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

{
if(c[j]<c[j+1])
{

temp=c[j];

c[j]=c[j+1];

c[j+1]=temp;

temp=w[j];

w[j]=w[j+1];

w[j+1]=temp;

temp=p[j];

p[j]=p[j+1];

p[j+1]=temp;

printf("\n the item are arranged...\n");

printf("\n\nitem\tweight\tprofit");

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

printf("\nx[%d]\t%.0f\t\t%.0f",i,w[i],p[i]);

knapsack(n,m,w,p);

getch();

void knapsack(int n,float m,float w[],float p[])

float x[15],u,profit=0.0,weight=0.0;

int i;

u=m;
for(i=0;i<n;i++)
x[i]=0.0;

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

if(w[i]>u)

break;

x[i]=1.0;

u=u-w[i];

if(i<n)

x[i]=u/w[i];

printf("\nsolution vector is");

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

printf("\n%d\t%.2f",i,x[i]);

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

w[i]=w[i]*x[i];

p[i]=p[i]*x[i];

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

profit=profit+p[i];

weight=weight+w[i];

printf("\nmaximum profit is");

printf("\n\t\t%.2f",profit);

printf("\nweight is");

printf("\n\t\t%.2f",weight);
}
11 N Queen problem using back tracking

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

int x[20];

int count;

void print_board(int n)

int i,j;

printf("\n\n solution %d : \n\n",++count);

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

printf("\t%d",i);

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

printf("\n\n%d",i);

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

if(x[i]==j)

printf("\tQ");

else

printf("\t-");

}
printf("\n press any key the contineu. .. ");
getch();

int place(int row, int column)

int i;

for(i=1;i<=row-1;i++)

if(x[i]==column)

return 0;

else

if(abs(x[i]-column)==abs(i-row))

return 0;

return 1;

void queen(int row, int n)

int column;

for(column=1;column<=n;column++)

if(place(row,column))

x[row]=column;

if(row==n)

print_board(n);

else

queen(row+1,n);
}
}

void main()

int n,i,j;

clrscr();

printf("\n\t program for n queen using backtracking");

printf("\n enter the nuber of queen");

scanf("%d",&n);

queen(1,n);

getch();

}
12 BUBBLE SORT

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,n,temp,a[100];
printf("enter the number");
scanf("%d",&n);
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])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("sorted array");
for(j=0;j<n;j++)
printf("%d",a[j]);
getch();
}
13. PROGRAM TO IMPLEMENT BUCKET SORT.

#include<stdio.h>

#include<conio.h>

void main()

int a[10][10],b[10],c[10],d[10];

inti,j,k=0,n,m,s;

clrscr();

printf("\n\n\tEnter the total number


of elements\n\n"); scanf("%d",&n);
printf("\n\nEnter the maximum number of digits in a number\n\n");

scanf("%d",&m);
printf("\n\nEnter
Elements\n\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(s=1;s<=m;s++)
{

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

{
for(j=0;j<10;j
++) a[i][j]=‐1;
}

for(i=0;i<n;i+
+) d[i]=b[i];
for(i=0;i<n;i+
+)
{

k=0;
While(k!=s)
{
c[i]=b[i]%1
0;
b[i]=b[i]/10
; k++;
}
}
for(i=0;i<n;i++)
{ j=0;
while(a[c[i]][j]!
=‐1) j++;
a[c[i]][j]=d[i];
}
for(i=0;i<n;i
++) d[i]=‐1;
k=0;
for(i=0;i<10;i++
) { j=0;
while(a[i][j]!=‐1
) { d[k]=a[i][j];
k++;
j++;
}
}
for(i=0;i<n;i+
+) b[i]=d[i];
}
printf("\n\nSorted
Elements Are\n\n");
for(i=0;i<n;i++)
printf("\n%d",d[i]);
getch();
}
OUTPUT‐
Enter the total number of elements

10
Enter the maximum number of digits in a number
2
Enter Elements
78 17 39 26 72 94 21 12 23 68
Sorted Elements Are
12 17 21 23 26 39 68 72 78 94
14. Travelling Salesman Problem

#include<stdio.h>

#include<conio.h>

#define max 10

typedef struct

int nodes[10];

int vertex;

int min;

path_node;

path_node tsp(int source,path_node list,int element[][max],int max_no_cities)

int i,j;

path_node new_list,new_path,new_min;

if(list.vertex==0)

new_min.min=element[source][1];

new_min.nodes[max_no_cities-1]=source;

new_min.vertex=max_no_cities;

return new_min;

for(i=0;i<list.vertex;i++)

new_list.vertex=0;

for(j=0;j<list.vertex;j++)

if(i!=j)
new_list.nodes[new_list.vertex++]=list.nodes[j];
new_path =tsp(list.nodes[i],new_list,element,max_no_cities);

new_path.min=element[source][list.nodes[i]]+new_path.min;

new_path.nodes[max_no_cities-list.vertex-1]=source;

if(i==0)

new_min=new_path;

else

if(new_path.min<new_min.min)

new_min=new_path;

return new_min;

void display(path_node path)

int i;

printf("\n\n the minimum cost is %d\n",path.min);

printf("\n the path is .... \n");

for(i=0;i<path.vertex;i++)

printf("%d--",path.nodes[i]);

printf("%d",path.nodes[0]);

main()

int i,j,element[max][max],max_no_cities;

path_node graph, path;

clrscr();

printf("\n how many number cities there");

scanf("%d",&max_no_cities);
if(max_no_cities==0)
{

printf("error there is no cities procesing");

else

for(i=1;i<=max_no_cities;i++)

for(j=1;j<=max_no_cities;j++)

if(i==j)

element[i][i]=0;

else

printf("enter the distance from cities %d to %d ",i,j);

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

if(i>1)

graph.nodes[i-2]=i;

graph.vertex=max_no_cities-1;

path=tsp(1,graph,element,max_no_cities);

display(path);}

getch();

return 1;

You might also like