0% found this document useful (0 votes)
20 views13 pages

DAA Lab Exp 1-5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

DAA Lab Exp 1-5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

DESIGN AND ANALYSIS OF ALGORITHM LAB

(BCS-553)

B. TECH
(3rd YEAR – ODD SEM)
(2024-25)

Student Name:

Roll No:

Branch & Section:

AJAY KUMAR GARG ENGINEERING COLLEGE


(Affiliated to Dr. APJ Abdul Kalam Technical University, Lucknow, UP, College Code - 027)
Website: https://www.akgec.ac.in/
GENERAL LABORATORY INSTRUCTIONS
1. Students are advised to come to the laboratory at least 5 minutes before (to the starting time),
those who come after 5 minutes will not be allowed into the lab.

2. Plan your task properly much before to the commencement, come prepared to the lab with the
synopsis / program / experiment details.

3.Student should enter into the laboratory with:

 Laboratory observation notes with all the details (Problem statement, Aim, Algorithm,
Procedure, Program, Expected Output, etc.,) filled in for the lab session.

 Laboratory Record updated up to the last session experiments and other utensils (if any)
needed in the lab.

4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.

5. Execute your task in the laboratory, and record the results / output in the lab observation note
book, and get certified by the concerned faculty.

6. All the students should be polite and cooperative with the laboratory staff, must maintain the
discipline and decency in the laboratory.

7. Computer labs are established with sophisticated and high-end branded systems, which should
be utilized properly.

8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviors with the staff and systems etc., will attract
severe punishment.

9. Students must take the permission of the faculty in case of any urgency to go out; if anybody
found loitering outside the lab / class without permission during working hours will be treated
seriously and punished appropriately.

10. Students should LOG OFF/ SHUT DOWN the computer system before he/she leaves the lab
after completing the task (experiment) in all aspects. He/she must ensure the system / seat is kept
properly.
LIST OF PROGRAMS

SUBJECT: DESIGN AND ANALYSIS OF ALGORITHM LAB CODE: BCS-553

S. FACULTY
NAME OF EXPERIMENT DATE
No. SIGNATURE
1 Write a program in C for Sequential Search.

2 Write a program in C for Binary Search.

3 Write a program in C for Insertion Sort.

4 Write a program in C for Selection Sort.

5 Write a program in C for Quick Sort.

6 Write a program in C for Heap Sort.

7 Write a program in C for Merge Sort.

8 Write a program in C to solve the fractional


Knapsack problem.
9 Write a program in C to find Minimum Spanning
Tree using Kruskal’s Algorithm
10 Write a program in C to implement N Queen
Problem using Backtracking
11 Write a program in C to perform Travelling
Salesman Problem
12 Write a program in C to solve LCS problem.

Beyond Syllabus

13 Write a program in C to implement Huffman-


code.
14 Write a program in C to implement Matrix-Chain-
Multiplication problem.
Experiment No. 1
Program Name:
Write a program in c for sequential search

Theory Concept:
The Linear Search, or sequential search, is simply examining each element in a list one by one
until the desired element is found. The Linear Search is not very efficient. If the item of data to
be found is at the end of the list, then all previous items must be read and checked before the
item that matches the search criteria is found.

Implementation:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,temp,ser,a[100],opt,beg,mid,end;
clrscr();
printf("enter the no of elements to be entered");
scanf("%d",&temp);
for(i=0;i<temp;i++)
{
printf("\n number %d ",i+1);
scanf("%d",&a[i]);
}
printf("enter the no to be searched");
scanf("%d",&ser);
for(i=0;i<temp;i++)
{
if(ser==a[i])
{
printf("\n number exists at location %d",i+1);
getch();
exit(0);
}
}
printf("The Number doesn't exist");
getch();
}
}
Output/Conclusion:

enter the no of elements to be entered 6


number 1 10
number 2 20
number 3 30
number 4 40
number 5 50
number 6 60

Enter The No to Be Searched 50

Number Exists at Location 5


Experiment No. 2
Program Name:
Write a program in c for binary search

Theory Concept:
This type of searching an element in the given array, the array should have elements in
sorted manner. Each time the given element is compared with the middle element of
given sorted array. And then it is decided to divide the further the given array.

Implementation:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int i,temp,ser,a[100],opt,beg=0,mid,end;
clrscr();
printf("enter the no of elements to be entered");
scanf("%d",&temp);
for(i=0;i<temp;i++)
{
printf("\n number %d ",i+1);
scanf("%d",&a[i]);
}
printf("enter the no to be searched");
scanf("%d",&ser);
end=temp,mid=(beg+end)/2;
while(beg<end)
{
if(ser>a[mid])
{
beg=mid;
}
if(ser<a[mid])
{
end=mid;
}
if(ser==a[mid])
{
printf("The Number exists at location %d",mid+1);
getch();
exit(0);
}
mid=(beg+end)/2;
}
printf("The Number doesn't exist");
getch();
}

Output/Conclusion:
enter the no of elements to be entered 6

number 1 10
number 2 20
number 3 30
number 4 40
number 5 50
number 6 60
enter the no to be searched 50
The number exists at location 5
Experiment No. 3
Program Name:
Write a program in c for Insertion Sort

Theory Concept:
Let a0, ..., an-1 be the sequence to be sorted. At the beginning and after each iteration of the
algorithm the sequence consists of two parts: the first part a0, ..., ai-1 is already sorted, the second
part ai, ..., an-1 is still unsorted (i 0, ..., n).
In order to insert element ai into the sorted part, it is compared with ai-1, ai-2 etc. When an
element aj with aj ai is found, ai is inserted behind it. If no such element is found, then ai is
inserted at the beginning of the sequence.
After inserting element ai the length of the sorted part has increased by one. In the next iteration,
ai+1 is inserted into the sorted part etc. While at the beginning the sorted part consists of element
a0 only, at the end it consists of all elements a0, ..., an-1.
Implementation:
#include<stdio.h>
#include<conio.h>
#define MAX 25
void main()
{
int array[MAX],n,i,j,key;
clrscr();
printf("Enter no. of elements :");
scanf("%d",&n);
printf("\nEnter data to be sorted :\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(j=1;j<n;j++)
{
i=j-1;
key=array[j];
while(i>-1&&key<array[i])
{
array[i+1]=array[i];
i=i-1;
}
array[i+1]=key;
}
printf("Sorted data is :\n");
for(i=0;i<n;i++)
printf("%d\n",array[i]);
getch();
}

Output/Conclusion
Enter no. of elements :8

5 7 0 3 4 2 6 1 (0)
5 7 0 3 4 2 6 1 (0)
0 5 7 3 4 2 6 1 (2)
0 3 5 7 4 2 6 1 (2)
0 3 4 5 7 2 6 1 (2)
0 2 3 4 5 7 6 1 (4)
0 2 3 4 5 6 7 1 (1)
0 1 2 3 4 5 6 7 (6)
Experiment No. 4
Program Name:

Write a program in c for Selection sort


Theory Concept:

In this sorting is we first find the smallest element and then exchanging it
with the first element in array. Then finding the second smallest element and then
placing it on second position in the array and so on.

Implementation:
#include<stdio.h>
#include<conio.h>
#define MAX 25
void main()
{
int array[MAX],n,i,j;
clrscr();
printf("Enter no. of elements :");
scanf("%d",&n);
printf("\nEnter data to be sorted :\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=1;i<n-1;i++)
for(j=n;j<i+1;j--)
if(array[i]>array[j])
{
array[i]^=array[j];
array[j]^=array[i];
array[i]^=array[j];
}
printf("Sorted data is :\n");
for(i=0;i<n;i++)
printf("%d\n",array[i]);
getch();
}
Output/Conclusion:
OUTPUT:

Enter no. of elements :6

Enter data to be sorted :


6
1
5
2
4
3
Sorted data is :
1
2
3
4
5
6
Experiment No. 5
Program Name:
Write a program in C for Quick sort

Theory Concept:
It is based on divide and conquer paradigm for sorting array. It is the best practical choice for
sorting because it is remarkably efficient on average case: its expected running time is of order of
nlogn. It works well in virtual memory.

Implementation:
#include<stdio.h>
#include<conio.h>
void quicksort(int [],int , int );
int partition(int [], int ,int );
void main()
{
int a[10],i,m;
clrscr();
printf("\nenter the size of the array : ");
scanf("%d",&m);
printf("\nenter the array element : ");
for(i=0;i<m;i++)
{ scanf("%d",&a[i]);
}
quicksort(a,0,m-1);
printf("\nsorted array is : ");
for(i=0;i<m;i++)
{ printf("%d",a[i]);
}
getch();
}
void quicksort(int a[], int p, int r)
{ int q,i;
if(p<r)
{ q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
int partition(int a[],int p, int r)
{ int x,i,j,t;
x=a[r];
i=p-1;
for(j=p;j<=(r-1);j++)
{ if(a[j]<=x)
{ i=i+1;
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
t=a[i+1];
a[i+1]=a[r];
a[r]=t;
return (i+1);
}

Output/Conclusion:
enter the size of the array : 8

enter the array element :7 5 4 8 9 1 2 10

sorted array is :1 2 4 5 7 8 9 10

You might also like