Professional Documents
Culture Documents
ENGINEERING
Lab Manual
KCS-553
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
OUTPUT‐
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
clrscr();
scanf("%d",&n);
}
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);
}
}
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];
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;
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;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&s);
for(i=0;i<n;i++)
if(s==a[i])
j=i+1;
flag=1;
if(flag==0)
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‐
#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 main()
int i,n,a[10];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
for(i=0;i<n;i++)
printf("\n %d",a[i]);
printf("\n");
getch();
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
for(i=0;i<nodes;i++)
tree[i]=0;
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++)
min_dist=g[i][j];
v1=i;
v2=j;
}
tree[v1]=tree[v2]=1;
total= total+min_dist;
void main()
int g[size][size],nodes;
clrscr();
scanf("%d",&nodes);
scanf("%d",&n) ;
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
g[i][j]=0;
for(i=0;i<n;i++)
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 main()
int i,j,n;
float p[15],w[15],c[15],temp,m;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",&w[i]);
flushall();
for(i=0;i<n;i++)
scanf("%f",&p[i]);
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\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();
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];
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("\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;
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 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;
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();
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();
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;
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;
int i;
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;
clrscr();
scanf("%d",&max_no_cities);
if(max_no_cities==0)
{
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
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;